Initial processing

This document describes the analysis of TCR repertoires for the manuscript "Unique roles of coreceptor-bound LCK in helper and cytotoxic T cellsby Horkova et al. The analysis workflow included:

  • sorting of CD4 and CD8 cells from lymph nodes and thymi of LckWT, LckCA and LckCA/KR mice,
  • isolation of RNA using RNA Clean & Concentrator-5 kit
  • preparation of TCR-enriched libraries using the NEBNext® Mouse Immune Sequencing Kit
  • sequencing the libraries on Illumina Miseq, 300bp paired-end reads
  • demultiplexing reads in Illumina BaseSpace
  • processing fastq files using the recommended pRESTO workflow on Galaxy with default parameters
  • aligning processed fastq files using MiXCR

Raw data are deposited in the Sequence Read Archive (PRJNA872031). Processed files can be downloaded download from Zenodo. The Zenodo archive contains the following files, which are needed to run this script:

  • merged outputs from MiXCR merged_TCR_repertoires.csv
  • metadata file metadata_Lck.csv
  • count tables with raw counts of CDR3 TRA and TRB sequences in each sample count_table_tra.csv, "count_table_trb.csv"
  • TRA and TRB repertoires prepared for processing with the Immunarch package immdata_tra.rds, immdata_trb.rds
merged_repertoires <- read.csv("merged_TCR_repertoires.csv")
md <- read.csv("metadata_Lck.csv")

head(md)
##   Exp       Organ Cell_type Mouse_strain num_id
## 1   1 Lymph nodes       CD8           WT      1
## 2   3 Lymph nodes       CD4           WT      2
## 3   2 Lymph nodes       CD8           WT      3
## 4   1 Lymph nodes       CD4           CA      4
## 5   3      Thymus       CD4           WT      5
## 6   3 Lymph nodes       CD8         CAKR      6

Count tables

counts_tra <- read_csv("count_table_tra.csv")
counts_tra$...1 <- NULL

counts_trb <- read_csv("count_table_trb.csv")
counts_trb$...1 <- NULL

TRB

Nice table for excel

md2_trb <- merged_repertoires %>% filter(chain == "TRB") %>% group_by(aaSeqCDR3) %>% slice_head(n = 1)

excel_count_table_trb <- counts_trb %>% left_join(md2_trb %>% select(allVHitsWithScore, allDHitsWithScore, allJHitsWithScore))

md_to_join <- md %>% mutate(new_name = paste(Cell_type, Organ, Mouse_strain, paste0("Exp0",Exp)))  %>% select(num_id, new_name) %>% arrange(num_id)

order <- match(as.numeric(colnames(counts_trb)[2:29]), md_to_join$num_id)


colnames(excel_count_table_trb)[2:29] <- pull(md_to_join, new_name)[order]

excel_count_table_trb2 <- excel_count_table_trb %>% dplyr::select(1, 30:32, 22, 12, 25, 11, 6, 26, 13, 18, 4, 28, 17, 14, 8, 23, 9, 7, 16, 24, 2, 21, 5, 29, 10, 20, 19, 3, 15, 27)

excel_count_table_trb2
## # A tibble: 426,800 × 32
##    aaSeqCDR3     allVH…¹ allDH…² allJH…³ CD4 L…⁴ CD4 L…⁵ CD4 L…⁶ CD4 L…⁷ CD4 L…⁸
##    <chr>         <chr>   <chr>   <chr>     <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 CASSDSAETLYF  TRBV13… <NA>    TRBJ2-…       7       1       6       1      12
##  2 CASSDAEQFF    TRBV13… <NA>    TRBJ2-…       0       0       5       0       1
##  3 CASSDAGYEQYF  TRBV13… <NA>    TRBJ2-…       0       1       2       0       3
##  4 CASSDWGGYAEQ… TRBV13… TRBD2*… TRBJ2-…       3       1       1       4       0
##  5 CASSDSGGQDTQ… TRBV13… TRBD2*… TRBJ2-…       1       0       2       1       0
##  6 CASRDRNTEVFF  TRBV13… TRBD1*… TRBJ1-…       6       1       3       3       0
##  7 CASSDNYEQYF   TRBV13… <NA>    TRBJ2-…       2       1       6       4       5
##  8 CASSDNYAEQFF  TRBV13… <NA>    TRBJ2-…       0       0       5       0       1
##  9 CASSDYEQYF    TRBV13… <NA>    TRBJ2-…       2       0       2       1       0
## 10 CASSDDEQYF    TRBV13… <NA>    TRBJ2-…       2       0       0       1       0
## # … with 426,790 more rows, 23 more variables:
## #   `CD4 Lymph nodes WT Exp02` <dbl>, `CD4 Lymph nodes WT Exp03` <dbl>,
## #   `CD4 Thymus CA Exp01` <dbl>, `CD4 Thymus CA Exp03` <dbl>,
## #   `CD4 Thymus CAKR Exp01` <dbl>, `CD4 Thymus CAKR Exp02` <dbl>,
## #   `CD4 Thymus CAKR Exp03` <dbl>, `CD4 Thymus WT Exp02` <dbl>,
## #   `CD4 Thymus WT Exp03` <dbl>, `CD8 Lymph nodes CA Exp02` <dbl>,
## #   `CD8 Lymph nodes CA Exp03` <dbl>, `CD8 Lymph nodes CAKR Exp02` <dbl>, …
colnames(excel_count_table_trb2)
##  [1] "aaSeqCDR3"                  "allVHitsWithScore"         
##  [3] "allDHitsWithScore"          "allJHitsWithScore"         
##  [5] "CD4 Lymph nodes CA Exp01"   "CD4 Lymph nodes CA Exp02"  
##  [7] "CD4 Lymph nodes CA Exp03"   "CD4 Lymph nodes CAKR Exp02"
##  [9] "CD4 Lymph nodes CAKR Exp03" "CD4 Lymph nodes WT Exp02"  
## [11] "CD4 Lymph nodes WT Exp03"   "CD4 Thymus CA Exp01"       
## [13] "CD4 Thymus CA Exp03"        "CD4 Thymus CAKR Exp01"     
## [15] "CD4 Thymus CAKR Exp02"      "CD4 Thymus CAKR Exp03"     
## [17] "CD4 Thymus WT Exp02"        "CD4 Thymus WT Exp03"       
## [19] "CD8 Lymph nodes CA Exp02"   "CD8 Lymph nodes CA Exp03"  
## [21] "CD8 Lymph nodes CAKR Exp02" "CD8 Lymph nodes CAKR Exp03"
## [23] "CD8 Lymph nodes WT Exp01"   "CD8 Lymph nodes WT Exp02"  
## [25] "CD8 Lymph nodes WT Exp03"   "CD8 Thymus CA Exp01"       
## [27] "CD8 Thymus CA Exp03"        "CD8 Thymus CAKR Exp02"     
## [29] "CD8 Thymus CAKR Exp03"      "CD8 Thymus WT Exp01"       
## [31] "CD8 Thymus WT Exp02"        "CD8 Thymus WT Exp03"
excel_count_table_trb3 <- excel_count_table_trb2 %>% mutate(nkt_trav11_traj18 = "no")

count_table_trb4 <- as.matrix(excel_count_table_trb3[,5:32])
rownames(count_table_trb4) <- excel_count_table_trb3$aaSeqCDR3


trb4_norm <- scale(count_table_trb4, center=FALSE, scale=colSums(count_table_trb4))

prop.table.trb <- cbind(trb4_norm, excel_count_table_trb %>% select(-starts_with("CD")) ) 
colnames(prop.table.trb)
##  [1] "CD4 Lymph nodes CA Exp01"   "CD4 Lymph nodes CA Exp02"  
##  [3] "CD4 Lymph nodes CA Exp03"   "CD4 Lymph nodes CAKR Exp02"
##  [5] "CD4 Lymph nodes CAKR Exp03" "CD4 Lymph nodes WT Exp02"  
##  [7] "CD4 Lymph nodes WT Exp03"   "CD4 Thymus CA Exp01"       
##  [9] "CD4 Thymus CA Exp03"        "CD4 Thymus CAKR Exp01"     
## [11] "CD4 Thymus CAKR Exp02"      "CD4 Thymus CAKR Exp03"     
## [13] "CD4 Thymus WT Exp02"        "CD4 Thymus WT Exp03"       
## [15] "CD8 Lymph nodes CA Exp02"   "CD8 Lymph nodes CA Exp03"  
## [17] "CD8 Lymph nodes CAKR Exp02" "CD8 Lymph nodes CAKR Exp03"
## [19] "CD8 Lymph nodes WT Exp01"   "CD8 Lymph nodes WT Exp02"  
## [21] "CD8 Lymph nodes WT Exp03"   "CD8 Thymus CA Exp01"       
## [23] "CD8 Thymus CA Exp03"        "CD8 Thymus CAKR Exp02"     
## [25] "CD8 Thymus CAKR Exp03"      "CD8 Thymus WT Exp01"       
## [27] "CD8 Thymus WT Exp02"        "CD8 Thymus WT Exp03"       
## [29] "aaSeqCDR3"                  "allVHitsWithScore"         
## [31] "allDHitsWithScore"          "allJHitsWithScore"
prop.table.trb2 <- prop.table.trb %>% dplyr::select(29, 31, 30, 32, 1:28)

TRA

md2_tra <- merged_repertoires %>% filter(chain == "TRA") %>% group_by(aaSeqCDR3) %>% slice_head(n = 1)

excel_count_table_tra <- counts_tra %>% left_join(md2_tra %>% select(allVHitsWithScore, allDHitsWithScore, allJHitsWithScore))

md_to_join <- md %>% mutate(new_name = paste(Cell_type, Organ, Mouse_strain, paste0("Exp0",Exp)))  %>% select(num_id, new_name) %>% arrange(num_id)

order <- match(as.numeric(colnames(counts_tra)[2:29]), md_to_join$num_id)

colnames(excel_count_table_tra)[2:29] <- pull(md_to_join, new_name)[order]
excel_count_table_tra2 <- excel_count_table_tra %>% dplyr::select(1, 31, 32, 30, 22, 12, 25, 11, 6, 26, 13, 18, 4, 28, 17, 14, 8, 23, 9, 7, 16, 24, 2, 21, 5, 29, 10, 20, 19, 3, 15, 27)

excel_count_table_tra3 <- excel_count_table_tra2 %>% mutate(nkt_trav11_traj18 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRAV11") & (grepl(allJHitsWithScore, pattern = "TRAJ18"))),"yes","no"))

excel_count_table_tra4 <- excel_count_table_tra3 %>% filter(nkt_trav11_traj18 == "no")

count_table_tra4 <- as.matrix(excel_count_table_tra4[,5:32])
rownames(count_table_tra4) <- excel_count_table_tra4$aaSeqCDR3

tra4_norm <- scale(count_table_tra4, center=FALSE, scale=colSums(count_table_tra4))

prop.table.tra <- cbind(tra4_norm, excel_count_table_tra4 %>% select(-starts_with("CD")) ) 

prop.table.tra2 <- prop.table.tra %>% dplyr::select(29, 31, 30, 32, 1:28)

less than 5 removed

CD4

Remove CDR3s with occurrence in less than 5 samples

# TRA
keep_tra_cd4 <- excel_count_table_tra %>% 
  mutate(nkt_trav11_traj18 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRAV11") | 
    (grepl(allJHitsWithScore, pattern = "TRAJ18"))),"yes","no")) %>% 
  filter(nkt_trav11_traj18 == "no") %>% 
  select(aaSeqCDR3, starts_with("CD4")) %>% 
  mutate_at(vars(starts_with("CD4")), .funs = binary) 
 
keep_tra_cd4$sum <- rowSums((keep_tra_cd4 %>% select(-aaSeqCDR3)))

keep_tra_cd4$keep <- ifelse(keep_tra_cd4$sum>4,1,0)

keep_tra_cd4_sequences <- pull(keep_tra_cd4 %>% filter(keep == 1), aaSeqCDR3)

# TRB
keep_trb_cd4 <- excel_count_table_trb %>% select(aaSeqCDR3, starts_with("CD4")) %>%  mutate_at(vars(starts_with("CD4")), .funs = binary) 
 
keep_trb_cd4$sum <- rowSums((keep_trb_cd4 %>% select(-aaSeqCDR3)))

keep_trb_cd4$keep <- ifelse(keep_trb_cd4$sum>4,1,0)

keep_trb_cd4_sequences <- pull(keep_trb_cd4 %>% filter(keep == 1), aaSeqCDR3)

CD8

Remove CDR3s with occurrence in less than 5 samples

# TRA
keep_tra_cd8 <- excel_count_table_tra %>% 
  mutate(nkt_trav11_traj18 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRAV11") | 
    (grepl(allJHitsWithScore, pattern = "TRAJ18"))),"yes","no")) %>% 
  filter(nkt_trav11_traj18 == "no") %>% 
  select(aaSeqCDR3, starts_with("CD8")) %>% 
  mutate_at(vars(starts_with("CD8")), .funs = binary) 

keep_tra_cd8$sum <- rowSums((keep_tra_cd8 %>% select(-aaSeqCDR3)))

keep_tra_cd8$keep <- ifelse(keep_tra_cd8$sum>4,1,0)

keep_tra_cd8_sequences <- pull(keep_tra_cd8 %>% filter(keep == 1), aaSeqCDR3)

# TRB
keep_trb_cd8 <- excel_count_table_trb %>% select(aaSeqCDR3, starts_with("CD8")) %>%  mutate_at(vars(starts_with("CD8")), .funs = binary) 
 
keep_trb_cd8$sum <- rowSums((keep_trb_cd8 %>% select(-aaSeqCDR3)))

keep_trb_cd8$keep <- ifelse(keep_trb_cd8$sum>4,1,0)

keep_trb_cd8_sequences <- pull(keep_trb_cd8 %>% filter(keep == 1), aaSeqCDR3)

TRB

excel_count_table_trb3_filter <- excel_count_table_trb2  %>% filter(aaSeqCDR3 %in% keep_trb_cd4_sequences | aaSeqCDR3 %in% keep_trb_cd8_sequences) 

count_table_trb4_filter <- as.matrix(excel_count_table_trb3_filter[,5:32])
rownames(count_table_trb4_filter) <- excel_count_table_trb3_filter$aaSeqCDR3

trb4_norm_filter <- scale(count_table_trb4_filter, center=FALSE, scale=colSums(count_table_trb4_filter))

prop.table.trb_filter <- cbind(trb4_norm_filter, excel_count_table_trb3_filter %>% select(-starts_with("CD")) )

TRA

excel_count_table_tra3_filter <- excel_count_table_tra3  %>% filter(aaSeqCDR3 %in% keep_tra_cd4_sequences | aaSeqCDR3 %in% keep_tra_cd8_sequences) %>% filter(nkt_trav11_traj18 == "no")

count_table_tra4_filter <- as.matrix(excel_count_table_tra3_filter[,5:32])
rownames(count_table_tra4_filter) <- excel_count_table_tra3_filter$aaSeqCDR3

tra4_norm_filter <- scale(count_table_tra4_filter, center=FALSE, scale=colSums(count_table_tra4_filter))

prop.table.tra_filter <- cbind(tra4_norm_filter, excel_count_table_tra3_filter %>% select(-starts_with("CD")) )

colnames(prop.table.tra_filter)
##  [1] "CD4 Lymph nodes CA Exp01"   "CD4 Lymph nodes CA Exp02"  
##  [3] "CD4 Lymph nodes CA Exp03"   "CD4 Lymph nodes CAKR Exp02"
##  [5] "CD4 Lymph nodes CAKR Exp03" "CD4 Lymph nodes WT Exp02"  
##  [7] "CD4 Lymph nodes WT Exp03"   "CD4 Thymus CA Exp01"       
##  [9] "CD4 Thymus CA Exp03"        "CD4 Thymus CAKR Exp01"     
## [11] "CD4 Thymus CAKR Exp02"      "CD4 Thymus CAKR Exp03"     
## [13] "CD4 Thymus WT Exp02"        "CD4 Thymus WT Exp03"       
## [15] "CD8 Lymph nodes CA Exp02"   "CD8 Lymph nodes CA Exp03"  
## [17] "CD8 Lymph nodes CAKR Exp02" "CD8 Lymph nodes CAKR Exp03"
## [19] "CD8 Lymph nodes WT Exp01"   "CD8 Lymph nodes WT Exp02"  
## [21] "CD8 Lymph nodes WT Exp03"   "CD8 Thymus CA Exp01"       
## [23] "CD8 Thymus CA Exp03"        "CD8 Thymus CAKR Exp02"     
## [25] "CD8 Thymus CAKR Exp03"      "CD8 Thymus WT Exp01"       
## [27] "CD8 Thymus WT Exp02"        "CD8 Thymus WT Exp03"       
## [29] "aaSeqCDR3"                  "allDHitsWithScore"         
## [31] "allJHitsWithScore"          "allVHitsWithScore"         
## [33] "nkt_trav11_traj18"

Gene segment usage analysis

immdata_tra <- readRDS("immdata_tra.rds")
immdata_trb <- readRDS("immdata_trb.rds")

TRA

### Thymus

cd4_thymus <- geneUsage(repFilter(immdata_tra, 
                                 .method = "by.meta", 
                                 .query = list(Organ = include('Thymus'), Cell_type = include('CD4')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trav")

vis(cd4_thymus, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_tra$meta) + ggtitle("TRA Thymus CD4") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70"))

#ggsave("./final_fig/gene_usage_new/cd4_thymus_tra_just1segment.png", width = 60, height = 12, units = "cm") 
#ggsave("./final_fig/gene_usage_new/cd4_thymus_tra_just1segment.svg", width = 60, height = 12, units = "cm") 


### LN
cd4_ln <- geneUsage(repFilter(immdata_tra, 
                                 .method = "by.meta", 
                                 .query = list(Organ = exclude('Thymus'), Cell_type = include('CD4')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trav")

vis(cd4_ln, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_tra$meta) + ggtitle("TRA LN CD4") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70"))

getwd()
## [1] "/home/vercanie/PRIMUS/data/48_lab/Project coreceptor-Lck interaction/!! Revision for Nature Immunology/VN TCR repertoires/Github/lck-tcrseq/lck-tcrseq/lck-tcrseq"
#ggsave("./final_fig/gene_usage_new/cd4_ln_tra_just1segment.png", width = 60, height = 12, units = "cm")
#ggsave("./final_fig/gene_usage_new/cd4_ln_tra_just1segment.svg", width = 60, height = 12, units = "cm")

cd8_thymus <- geneUsage(repFilter(immdata_tra, 
                                 .method = "by.meta", 
                                 .query = list(Organ = include('Thymus'), Cell_type = include('CD8')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trav")

vis(cd8_thymus, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_tra$meta) + ggtitle("TRA Thymus CD8") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70"))

#ggsave("./final_fig/gene_usage_new/cd8_thymus_tra_just1segment.png", width = 60, height = 12, units = "cm")
#ggsave("./final_fig/gene_usage_new/cd8_thymus_tra_just1segment.svg", width = 60, height = 12, units = "cm")


cd8_ln <- geneUsage(repFilter(immdata_tra, 
                                 .method = "by.meta", 
                                 .query = list(Organ = exclude('Thymus'), Cell_type = include('CD8')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trav")

vis(cd8_ln, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_tra$meta) + ggtitle("TRA LN CD8") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70"))

#ggsave("./final_fig/gene_usage_new/cd8_ln_tra_just1segment.png", width = 60, height = 12, units = "cm")
#ggsave("./final_fig/gene_usage_new/cd8_ln_tra_just1segment.svg", width = 60, height = 12, units = "cm")

TRB

### Thymus

cd4_thymus <- geneUsage(repFilter(immdata_trb, 
                                 .method = "by.meta", 
                                 .query = list(Organ = include('Thymus'), Cell_type = include('CD4')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trbv")

vis(cd4_thymus, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_trb$meta, .test = F) + ggtitle("TRB Thymus CD4") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70")) 

#ggsave("./final_fig/gene_usage_new/cd4_thymus_trb_just1segment.png", width = 20, height = 10, units = "cm") 
#ggsave("./final_fig/gene_usage_new/cd4_thymus_trb_just1segment.svg", width = 20, height = 10, units = "cm")

### LN
cd4_ln <- geneUsage(repFilter(immdata_trb, 
                                 .method = "by.meta", 
                                 .query = list(Organ = exclude('Thymus'), Cell_type = include('CD4')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trbv")

vis(cd4_ln, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_trb$meta, .test = F) + ggtitle("TRB LN CD4") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70"))

getwd()
## [1] "/home/vercanie/PRIMUS/data/48_lab/Project coreceptor-Lck interaction/!! Revision for Nature Immunology/VN TCR repertoires/Github/lck-tcrseq/lck-tcrseq/lck-tcrseq"
#ggsave("./final_fig/gene_usage_new/cd4_ln_trb_just1segment.png", width = 20, height = 10, units = "cm")
#ggsave("./final_fig/gene_usage_new/cd4_ln_trb_just1segment.svg", width = 20, height = 10, units = "cm")


cd8_thymus <- geneUsage(repFilter(immdata_trb, 
                                 .method = "by.meta", 
                                 .query = list(Organ = include('Thymus'), Cell_type = include('CD8')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trbv")

vis(cd8_thymus, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_trb$meta, .test = F) + ggtitle("TRB Thymus CD8") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70"))

#ggsave("./final_fig/gene_usage_new/cd8_thymus_trb_just1segment.png", width = 20, height = 10, units = "cm")
#ggsave("./final_fig/gene_usage_new/cd8_thymus_trb_just1segment.svg", width = 20, height = 10, units = "cm")


cd8_ln <- geneUsage(repFilter(immdata_trb, 
                                 .method = "by.meta", 
                                 .query = list(Organ = exclude('Thymus'), Cell_type = include('CD8')))$data, .norm = T, .ambig = "maj", .quant = "count", .gene = "musmus.trbv")

vis(cd8_ln, .by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_trb$meta, .test = F) + ggtitle("TRB LN CD8") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70"))

#ggsave("./final_fig/gene_usage_new/cd8_ln_trb_just1segment.png", width = 20, height = 10, units = "cm")
#ggsave("./final_fig/gene_usage_new/cd8_ln_trb_just1segment.svg", width = 20, height = 10, units = "cm")

Analysis of NKT cells

Next, we will identify gene segments that are typical for invariant NKT cells. These segments are TRAV11 and TRAJ18 for TRA and TRBV1, TRBV13-2 and TRBV29 for TRB. Please, note that some of the included TRB chains are used by conventional cells as well.

Counts of NKT gene segments:

# NKT analysis
merged_repertoires <- merged_repertoires %>% 
  mutate(is_nkt = if_else(
    (grepl(allVHitsWithScore, pattern = "TRAV11")) & (grepl(allJHitsWithScore, pattern = "TRAJ18")) |
    (grepl(allVHitsWithScore, pattern = "TRBV13-2")) |
      (grepl(allVHitsWithScore, pattern = "TRBV1\\*")) |
      (grepl(allVHitsWithScore, pattern = "TRBV29"))  ,"yes","no")) %>% 
  mutate(nkt_trav11_traj18 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRAV11") & (grepl(allJHitsWithScore, pattern = "TRAJ18"))),"yes","no")) %>% mutate(nkt_trbv13_2 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRBV13-2")),"yes","no")) %>% mutate(nkt_trbv29 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRBV29")),"yes","no")) %>% mutate(nkt_trbv1 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRBV1\\*")),"yes","no"))

md <- md %>% select(Exp, Organ, Cell_type, Mouse_strain, num_id) %>% mutate_at(vars("num_id"), as.numeric)

# All TRB NKT
nkt_table_trb <- merged_repertoires %>% 
   filter(chain == "TRB") %>% 
 mutate(new_name = paste(Exp, Organ, Cell_type, Mouse_strain)) %>% 
  dplyr::select(new_name, cloneCount, is_nkt, num_id) %>% 
  uncount(cloneCount) %>% 
  group_by(num_id, is_nkt) %>% 
    summarise(n = n()) %>% 
mutate(freq = n / sum(n)) %>% dplyr::filter(is_nkt == "yes") %>% 
  ungroup %>% 
  left_join(md) %>% 
  unique %>%
   mutate(sample_type = paste(Cell_type, Organ, Mouse_strain)) 

# TRAV11 TRAJ18
nkt_table_trav11_traj18 <- merged_repertoires %>% 
   filter(chain == "TRA") %>% 
 mutate(new_name = paste(Exp, Organ, Cell_type, Mouse_strain)) %>% 
  dplyr::select(new_name, cloneCount, nkt_trav11_traj18, num_id) %>% 
  uncount(cloneCount) %>% 
  group_by(num_id, nkt_trav11_traj18) %>% 
    summarise(n = n()) %>% 
mutate(freq = n / sum(n)) %>% dplyr::filter(nkt_trav11_traj18 == "yes") %>% 
  ungroup %>% 
  left_join(md) %>% 
  unique %>%
   mutate(sample_type = paste(Cell_type, Organ, Mouse_strain)) 

# TRBV1
nkt_table_trbv1 <- merged_repertoires %>% 
   filter(chain == "TRB") %>% 
 mutate(new_name = paste(Exp, Organ, Cell_type, Mouse_strain)) %>% 
  dplyr::select(new_name, cloneCount, nkt_trbv1, num_id) %>% 
  uncount(cloneCount) %>% 
  group_by(num_id, nkt_trbv1) %>% 
    summarise(n = n()) %>% 
mutate(freq = n / sum(n)) %>% dplyr::filter(nkt_trbv1 == "yes") %>% 
  ungroup %>% 
  left_join(md) %>% 
  unique %>%
   mutate(sample_type = paste(Cell_type, Organ, Mouse_strain)) 

# TRBV13-2
nkt_table_trbv13_2 <- merged_repertoires %>% 
   filter(chain == "TRB") %>% 
 mutate(new_name = paste(Exp, Organ, Cell_type, Mouse_strain)) %>% 
  dplyr::select(new_name, cloneCount, nkt_trbv13_2, num_id) %>% 
  uncount(cloneCount) %>% 
  group_by(num_id, nkt_trbv13_2) %>% 
    summarise(n = n()) %>% 
mutate(freq = n / sum(n)) %>% dplyr::filter(nkt_trbv13_2 == "yes") %>% 
  ungroup %>% 
  left_join(md) %>% 
  unique %>%
   mutate(sample_type = paste(Cell_type, Organ, Mouse_strain)) 


# TRBV29
nkt_table_trbv29 <- merged_repertoires %>% 
   filter(chain == "TRB") %>% 
 mutate(new_name = paste(Exp, Organ, Cell_type, Mouse_strain)) %>% 
  dplyr::select(new_name, cloneCount, nkt_trbv29, num_id) %>% 
  uncount(cloneCount) %>% 
  group_by(num_id, nkt_trbv29) %>% 
    summarise(n = n()) %>% 
mutate(freq = n / sum(n)) %>% dplyr::filter(nkt_trbv29 == "yes") %>% 
  ungroup %>% 
  left_join(md) %>% 
  unique %>%
   mutate(sample_type = paste(Cell_type, Organ, Mouse_strain)) 

levels_cd4 <- c("CD4 Thymus WT", "CD4 Thymus CA", "CD4 Thymus CAKR", "CD4 Lymph nodes WT", "CD4 Lymph nodes CA", "CD4 Lymph nodes CAKR")
levels_cd8 <- c("CD8 Thymus WT", "CD8 Thymus CA", "CD8 Thymus CAKR", "CD8 Lymph nodes WT", "CD8 Lymph nodes CA", "CD8 Lymph nodes CAKR")
nkt_table_trb$nkt_sample <- "all_nkt_trb"
nkt_table_trav11_traj18$nkt_sample <- "trav11_traj18"
nkt_table_trbv1$nkt_sample <- "trbv1"
nkt_table_trbv13_2$nkt_sample <- "trbv13_2"
nkt_table_trbv29$nkt_sample <- "trbv29"

nkt_table <- cbind(nkt_table_trb$num_id, nkt_table_trb$sample_type, nkt_table_trb$Exp, format(nkt_table_trb$freq, digits = 2), 
                   format(nkt_table_trav11_traj18$freq, digits = 2),
format(nkt_table_trbv1$freq, digits = 2), 
format(nkt_table_trbv13_2$freq, digits = 2),
                   format(nkt_table_trbv29$freq, digits = 2)) %>% as.data.frame

colnames(nkt_table) <- c("num_id","Sample","Exp","All TRB NKT", "TRAV11-TRAJ18", "TRBV1","TRBV13-2","TRBV29")
nkt_table <- nkt_table %>% mutate(Sample = paste(Sample, Exp)) %>% select(-num_id, -Exp) %>% arrange(Sample)

kable(nkt_table, format = "html") %>%
  kable_styling(full_width = F, font_size = 11, 
                bootstrap_options = c("striped", "hover", "condensed", "responsive"))
Sample All TRB NKT TRAV11-TRAJ18 TRBV1 TRBV13-2 TRBV29
CD4 Lymph nodes CA 1 0.18 0.02574 0.032 0.120 0.027
CD4 Lymph nodes CA 2 0.19 0.02326 0.031 0.124 0.031
CD4 Lymph nodes CA 3 0.16 0.02046 0.024 0.109 0.028
CD4 Lymph nodes CAKR 2 0.19 0.01704 0.031 0.127 0.029
CD4 Lymph nodes CAKR 3 0.18 0.02357 0.034 0.120 0.030
CD4 Lymph nodes WT 2 0.17 0.00474 0.028 0.114 0.029
CD4 Lymph nodes WT 3 0.17 0.00307 0.031 0.111 0.029
CD4 Thymus CA 1 0.36 0.25798 0.032 0.228 0.101
CD4 Thymus CA 3 0.38 0.26069 0.033 0.219 0.124
CD4 Thymus CAKR 1 0.30 0.22058 0.033 0.205 0.065
CD4 Thymus CAKR 2 0.32 0.38268 0.069 0.185 0.068
CD4 Thymus CAKR 3 0.38 0.23262 0.042 0.249 0.084
CD4 Thymus WT 2 0.23 0.08824 0.034 0.136 0.058
CD4 Thymus WT 3 0.19 0.06685 0.038 0.107 0.045
CD8 Lymph nodes CA 2 0.21 0.00076 0.028 0.084 0.099
CD8 Lymph nodes CA 3 0.20 0.00137 0.026 0.085 0.091
CD8 Lymph nodes CAKR 2 0.24 0.00084 0.030 0.122 0.087
CD8 Lymph nodes CAKR 3 0.20 0.00080 0.027 0.089 0.082
CD8 Lymph nodes WT 1 0.23 0.00117 0.028 0.108 0.096
CD8 Lymph nodes WT 2 0.21 0.00051 0.024 0.091 0.098
CD8 Lymph nodes WT 3 0.21 0.00091 0.024 0.089 0.095
CD8 Thymus CA 1 0.22 0.00219 0.029 0.116 0.078
CD8 Thymus CA 3 0.19 0.00102 0.024 0.084 0.078
CD8 Thymus CAKR 2 0.23 0.00521 0.028 0.124 0.076
CD8 Thymus CAKR 3 0.19 0.00188 0.032 0.105 0.056
CD8 Thymus WT 1 0.23 0.00126 0.022 0.111 0.101
CD8 Thymus WT 2 0.26 0.00115 0.027 0.129 0.102
CD8 Thymus WT 3 0.20 0.00126 0.023 0.086 0.090

All NKT

## CD4
p01 <- nkt_table_trb %>% 
  filter(Cell_type == "CD4") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd4))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("NKT segments in CD4 TRB") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_all_nkt_trb_cd4.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_all_nkt_trb_cd4.eps", width = 8, height = 8, units = "cm")

## CD8
p02 <- nkt_table_trb %>% 
  filter(Cell_type == "CD8") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd8))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("NKT segments in CD8 TRB") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_all_nkt_trb_cd8.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_all_nkt_trb_cd8.eps", width = 8, height = 8, units = "cm")

p01 + p02

TRAV11 TRAJ18

## CD4
p03 <- nkt_table_trav11_traj18 %>% 
  filter(Cell_type == "CD4") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd4))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("TRAV11-TRAJ18 segments in CD4 cells") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trav11_traj18_tra_cd4.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trav11_traj18_tra_cd4.eps", width = 8, height = 8, units = "cm")

## CD8
p04 <- nkt_table_trav11_traj18 %>% 
  filter(Cell_type == "CD8") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd8))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("TRAV11-TRAJ18 segments in CD8 cells") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,1)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trav11_traj18_tra_cd8.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trav11_traj18_tra_cd8.eps", width = 8, height = 8, units = "cm")

p03 + p04

TRBV1

## CD4
p05 <- nkt_table_trbv1 %>% 
  filter(Cell_type == "CD4") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd4))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("CD4 TRBV1") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trbv1_cd4.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trbv1_cd4.eps", width = 8, height = 8, units = "cm")

## CD8
p05 <- nkt_table_trbv1 %>% 
  filter(Cell_type == "CD8") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd8))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("CD8 TRBV1") + 
  theme_classic() + 
  ggtheme() + 
  
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trbv1_cd8.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trbv1_cd8.eps", width = 8, height = 8, units = "cm")

p05 + p06
## Error in eval(expr, envir, enclos): object 'p06' not found

TRBV13-2

## CD4
p07 <- nkt_table_trbv13_2 %>% 
  filter(Cell_type == "CD4") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd4))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("TRBV13-2 segments in CD4 cells") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trbv13_2_cd4.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trbv13_2_cd4.eps", width = 8, height = 8, units = "cm")

## CD8
p08 <- nkt_table_trbv13_2 %>% 
  filter(Cell_type == "CD8") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd8))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("TRBV13-2 segments in CD8 cells") + 
  theme_classic() + 
  ggtheme() + 
  
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trbv13_2_cd8.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trbv13_2_cd8.eps", width = 8, height = 8, units = "cm")

p07 + p08

TRBV29

## CD4
p09 <- nkt_table_trbv29 %>% 
  filter(Cell_type == "CD4") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd4))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("CD4 TRBV29") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trbv29_cd4.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trbv29_cd4.eps", width = 8, height = 8, units = "cm")

## CD8
p10 <- nkt_table_trbv29 %>% 
  filter(Cell_type == "CD8") %>% 
  ggplot(aes(y = freq*100, x = factor(sample_type, levels = levels_cd8))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Percentage of all chains") + 
  ggtitle("CD8 TRBV29") + 
  theme_classic() + 
  ggtheme() + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
  
#ggsave("./final_fig/nkt/pct_nkt_trbv29_cd8.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/nkt/pct_nkt_trbv29_cd8.eps", width = 8, height = 8, units = "cm")

p09 + p10

PCA

prop.table.tra2_filter.cd4 <- prop.table.tra_filter[,1:14]
prop.table.tra2_filter.cd4$sum <- rowSums(prop.table.tra2_filter.cd4)
prop.table.tra2_filter.cd4 <- prop.table.tra2_filter.cd4 %>% filter(sum >0) %>% select(-sum)

prop.table.trb2_filter.cd4 <- prop.table.trb_filter[,1:14]
prop.table.trb2_filter.cd4$sum <- rowSums(prop.table.trb2_filter.cd4)
prop.table.trb2_filter.cd4 <- prop.table.trb2_filter.cd4 %>% filter(sum >0) %>% select(-sum)

prop.table.tra2_filter.cd8 <- prop.table.tra_filter[,15:28]
prop.table.tra2_filter.cd8$sum <- rowSums(prop.table.tra2_filter.cd8)
prop.table.tra2_filter.cd8 <- prop.table.tra2_filter.cd8 %>% filter(sum >0) %>% select(-sum)

prop.table.trb2_filter.cd8 <- prop.table.trb_filter[,15:28]
prop.table.trb2_filter.cd8$sum <- rowSums(prop.table.trb2_filter.cd8)
prop.table.trb2_filter.cd8 <- prop.table.trb2_filter.cd8 %>% filter(sum >0) %>% select(-sum)

TRA + TRB together

CD4

colnames(prop.table.tra2_filter.cd4) == colnames(prop.table.trb2_filter.cd4)
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
prop.table.tra2_filter.merge <- rbind(prop.table.tra2_filter.cd4, prop.table.trb2_filter.cd4)
  
res.pca.merge.cd4 <- prcomp(t(prop.table.tra2_filter.merge), scale = TRUE, center = T)

mdres.pca.merge.cd4  <-  colnames(prop.table.tra2_filter.merge[,1:14])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))

fviz_pca_ind(res.pca.merge.cd4,
             col.ind = as.factor(mdres.pca.merge.cd4$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.merge.cd4$Organ),
                  color = as.factor(mdres.pca.merge.cd4$Strain))) + 
  scale_shape_manual(values = c(8,8,8,15,15)) +
  ggtheme() +
  theme(axis.text.x = element_text(angle = 0))

#ggsave("final_fig/pca/cd4.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd4.svg", width = 2.6, height = 1.7)

Zoom

fviz_pca_ind(res.pca.merge.cd4,
             col.ind = as.factor(mdres.pca.merge.cd4$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.merge.cd4$Organ),
                  color = as.factor(mdres.pca.merge.cd4$Strain))) + 
  scale_shape_manual(values = c(8,8,8,15,15)) +
  xlim(c(-30,0)) + ylim(-2,15) +
  ggtheme() +
  theme(axis.text.x = element_text(angle = 0))

#ggsave("final_fig/pca/cd4_zoom.png", width = 2.2, height = 1.4)
#ggsave("final_fig/pca/cd4_zoom.svg", width = 2.2, height = 1.4)

CD8

colnames(prop.table.tra2_filter.cd8) == colnames(prop.table.trb2_filter.cd8)
##  [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
prop.table.tra2_filter.merge <- rbind(prop.table.tra2_filter.cd8, prop.table.trb2_filter.cd8)
  
res.pca.merge.cd8 <- prcomp(t(prop.table.tra2_filter.merge), scale = TRUE, center = T)

mdres.pca.merge.cd8  <-  colnames(prop.table.tra2_filter.merge[,1:14])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
  

fviz_pca_ind(res.pca.merge.cd8,
             col.ind = as.factor(mdres.pca.merge.cd8$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.merge.cd8$Organ),
                  color = as.factor(mdres.pca.merge.cd8$Strain))) + 
  scale_shape_manual(values = c(8,8,8,15,15)) +
  ggtheme()

#ggsave("final_fig/pca/cd8.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd8.svg", width = 2.6, height = 1.7)

Zoom

fviz_pca_ind(res.pca.merge.cd8,
             col.ind = as.factor(mdres.pca.merge.cd8$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.merge.cd8$Organ),
                  color = as.factor(mdres.pca.merge.cd8$Strain))) + 
  scale_shape_manual(values = c(8,8,8,15,15)) +
  xlim(c(-32,-18)) + ylim(-4,5) +
  ggtheme()

#ggsave("final_fig/pca/cd8_zoom.png", width = 2.2, height = 1.4)
#ggsave("final_fig/pca/cd8_zoom.svg", width = 2.2, height = 1.4)

TRA

res.pca <- prcomp(t(prop.table.tra_filter[,1:28]), scale = TRUE)


md  <-  colnames(prop.table.tra_filter[,1:28])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
  
fviz_pca_ind(res.pca,
             col.ind = as.factor(md$Cell_type), # color by groups
             legend.title = "Groups",
             repel = TRUE)

CD4

## All

prop.table.tra2_filter.cd4 <- prop.table.tra_filter[,1:14]
prop.table.tra2_filter.cd4$sum <- rowSums(prop.table.tra2_filter.cd4)

prop.table.tra2_filter.cd4 <- prop.table.tra2_filter.cd4 %>% filter(sum >0) %>% select(-sum)

res.pca.tra.cd4 <- prcomp(t(prop.table.tra2_filter.cd4), scale = TRUE, center = T)
fviz_eig(res.pca.tra.cd4)

mdres.pca.tra.cd4  <-  colnames(prop.table.tra2_filter[,1:14])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
# LN
prop.table.tra2_filter %>% colnames
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd4.ln <- prop.table.tra2_filter[,1:7]
## Error in eval(expr, envir, enclos): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd4.ln$sum <- rowSums(prop.table.tra2_filter.cd4.ln)
## Error in is.data.frame(x): object 'prop.table.tra2_filter.cd4.ln' not found
prop.table.tra2_filter.cd4.ln <- prop.table.tra2_filter.cd4.ln %>% filter(sum >0) %>% select(-sum)
## Error in filter(., sum > 0): object 'prop.table.tra2_filter.cd4.ln' not found
res.pca.tra.cd4.ln <- prcomp(t(prop.table.tra2_filter.cd4.ln), scale = TRUE, center = T)
## Error in t(prop.table.tra2_filter.cd4.ln): object 'prop.table.tra2_filter.cd4.ln' not found
mdres.pca.tra.cd4.ln  <-  colnames(prop.table.tra2_filter[,1:7])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
fviz_pca_ind(res.pca.tra.cd4.ln,
             col.ind = as.factor(mdres.pca.tra.cd4.ln$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.tra.cd4.ln$Organ),
                  color = as.factor(mdres.pca.tra.cd4.ln$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") 
## Error in .get_facto_class(X): object 'res.pca.tra.cd4.ln' not found
#ggsave("final_fig/pca/cd4_tra_ln.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd4_tra_ln.svg", width = 2.6, height = 1.7)

# Thymus

prop.table.tra2_filter.cd4.thy <- prop.table.tra2_filter[,8:14]
## Error in eval(expr, envir, enclos): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd4.thy$sum <- rowSums(prop.table.tra2_filter.cd4.thy)
## Error in is.data.frame(x): object 'prop.table.tra2_filter.cd4.thy' not found
prop.table.tra2_filter.cd4.thy <- prop.table.tra2_filter.cd4.thy %>% filter(sum >0) %>% select(-sum)
## Error in filter(., sum > 0): object 'prop.table.tra2_filter.cd4.thy' not found
res.pca.tra.cd4.thy <- prcomp(t(prop.table.tra2_filter.cd4.thy), scale = TRUE, center = T)
## Error in t(prop.table.tra2_filter.cd4.thy): object 'prop.table.tra2_filter.cd4.thy' not found
mdres.pca.tra.cd4.thy  <-  colnames(prop.table.tra2_filter[,8:14])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
fviz_pca_ind(res.pca.tra.cd4.thy,
             col.ind = as.factor(mdres.pca.tra.cd4.thy$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.tra.cd4.thy$Organ),
                  color = as.factor(mdres.pca.tra.cd4.thy$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") +
  ggtheme()
## Error in .get_facto_class(X): object 'res.pca.tra.cd4.thy' not found
#ggsave("final_fig/pca/cd4_tra_thy.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd4_tra_thy.svg", width = 2.6, height = 1.7)

CD8

## All

prop.table.tra2_filter.cd8 <- prop.table.tra2_filter[,15:28]
## Error in eval(expr, envir, enclos): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd8$sum <- rowSums(prop.table.tra2_filter.cd8)

prop.table.tra2_filter.cd8 <- prop.table.tra2_filter.cd8 %>% filter(sum >0) %>% select(-sum)

res.pca.tra.cd8 <- prcomp(t(prop.table.tra2_filter.cd8), scale = TRUE, center = T)

mdres.pca.tra.cd8  <-  colnames(prop.table.tra2_filter[,15:28])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
# LN
prop.table.tra2_filter %>% colnames
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd8.ln <- prop.table.tra2_filter[,15:21]
## Error in eval(expr, envir, enclos): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd8.ln$sum <- rowSums(prop.table.tra2_filter.cd8.ln)
## Error in is.data.frame(x): object 'prop.table.tra2_filter.cd8.ln' not found
prop.table.tra2_filter.cd8.ln <- prop.table.tra2_filter.cd8.ln %>% filter(sum >0) %>% select(-sum)
## Error in filter(., sum > 0): object 'prop.table.tra2_filter.cd8.ln' not found
res.pca.tra.cd8.ln <- prcomp(t(prop.table.tra2_filter.cd8.ln), scale = TRUE, center = T)
## Error in t(prop.table.tra2_filter.cd8.ln): object 'prop.table.tra2_filter.cd8.ln' not found
mdres.pca.tra.cd8.ln  <-  colnames(prop.table.tra2_filter[,15:21])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
fviz_pca_ind(res.pca.tra.cd8.ln,
             col.ind = as.factor(mdres.pca.tra.cd8.ln$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.tra.cd8.ln$Organ),
                  color = as.factor(mdres.pca.tra.cd8.ln$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") 
## Error in .get_facto_class(X): object 'res.pca.tra.cd8.ln' not found
#ggsave("final_fig/pca/cd8_tra_ln.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd8_tra_ln.svg", width = 2.6, height = 1.7)

# Thymus

prop.table.tra2_filter %>% colnames
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd8.thy <- prop.table.tra2_filter[,22:28]
## Error in eval(expr, envir, enclos): object 'prop.table.tra2_filter' not found
prop.table.tra2_filter.cd8.thy$sum <- rowSums(prop.table.tra2_filter.cd8.thy)
## Error in is.data.frame(x): object 'prop.table.tra2_filter.cd8.thy' not found
prop.table.tra2_filter.cd8.thy <- prop.table.tra2_filter.cd8.thy %>% filter(sum >0) %>% select(-sum)
## Error in filter(., sum > 0): object 'prop.table.tra2_filter.cd8.thy' not found
res.pca.tra.cd8.thy <- prcomp(t(prop.table.tra2_filter.cd8.thy), scale = TRUE, center = T)
## Error in t(prop.table.tra2_filter.cd8.thy): object 'prop.table.tra2_filter.cd8.thy' not found
mdres.pca.tra.cd8.thy  <-  colnames(prop.table.tra2_filter[,22:28])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
## Error in is.data.frame(x): object 'prop.table.tra2_filter' not found
fviz_pca_ind(res.pca.tra.cd8.thy,
             col.ind = as.factor(mdres.pca.tra.cd8.thy$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.tra.cd8.thy$Organ),
                  color = as.factor(mdres.pca.tra.cd8.thy$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") 
## Error in .get_facto_class(X): object 'res.pca.tra.cd8.thy' not found
#ggsave("final_fig/pca/cd8_tra_thy.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd8_tra_thy.svg", width = 2.6, height = 1.7)

TRB

res.pca.trb <- prcomp(t(prop.table.trb_filter[,5:32]), scale = TRUE)
## Error in colMeans(x, na.rm = TRUE): 'x' must be numeric
md_trb  <-  colnames(prop.table.trb_filter[,5:32])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))

CD4

## All
colnames(prop.table.trb2_filter)
## Error in is.data.frame(x): object 'prop.table.trb2_filter' not found
prop.table.trb2_filter.cd4 <- prop.table.trb_filter[,5:18]
prop.table.trb2_filter.cd4$sum <- rowSums(prop.table.trb2_filter.cd4)

prop.table.trb2_filter.cd4 <- prop.table.trb2_filter.cd4 %>% filter(sum >0) %>% select(-sum)

res.pca.trb.cd4 <- prcomp(t(prop.table.trb2_filter.cd4), scale = TRUE, center = T)

mdres.pca.trb.cd4  <-  colnames(prop.table.trb_filter[,5:18])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
 

# LN
prop.table.trb_filter %>% colnames
##  [1] "CD4 Lymph nodes CA Exp01"   "CD4 Lymph nodes CA Exp02"  
##  [3] "CD4 Lymph nodes CA Exp03"   "CD4 Lymph nodes CAKR Exp02"
##  [5] "CD4 Lymph nodes CAKR Exp03" "CD4 Lymph nodes WT Exp02"  
##  [7] "CD4 Lymph nodes WT Exp03"   "CD4 Thymus CA Exp01"       
##  [9] "CD4 Thymus CA Exp03"        "CD4 Thymus CAKR Exp01"     
## [11] "CD4 Thymus CAKR Exp02"      "CD4 Thymus CAKR Exp03"     
## [13] "CD4 Thymus WT Exp02"        "CD4 Thymus WT Exp03"       
## [15] "CD8 Lymph nodes CA Exp02"   "CD8 Lymph nodes CA Exp03"  
## [17] "CD8 Lymph nodes CAKR Exp02" "CD8 Lymph nodes CAKR Exp03"
## [19] "CD8 Lymph nodes WT Exp01"   "CD8 Lymph nodes WT Exp02"  
## [21] "CD8 Lymph nodes WT Exp03"   "CD8 Thymus CA Exp01"       
## [23] "CD8 Thymus CA Exp03"        "CD8 Thymus CAKR Exp02"     
## [25] "CD8 Thymus CAKR Exp03"      "CD8 Thymus WT Exp01"       
## [27] "CD8 Thymus WT Exp02"        "CD8 Thymus WT Exp03"       
## [29] "aaSeqCDR3"                  "allVHitsWithScore"         
## [31] "allDHitsWithScore"          "allJHitsWithScore"
prop.table.trb2_filter.cd4.ln <- prop.table.trb_filter[,5:11]
prop.table.trb2_filter.cd4.ln$sum <- rowSums(prop.table.trb2_filter.cd4.ln)

prop.table.trb2_filter.cd4.ln <- prop.table.trb2_filter.cd4.ln %>% filter(sum >0) %>% select(-sum)

res.pca.trb.cd4.ln <- prcomp(t(prop.table.trb2_filter.cd4.ln), scale = TRUE, center = T)

mdres.pca.trb.cd4.ln  <-  colnames(prop.table.trb_filter[,5:11])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
  

fviz_pca_ind(res.pca.trb.cd4.ln,
             col.ind = as.factor(mdres.pca.trb.cd4.ln$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.trb.cd4.ln$Organ),
                  color = as.factor(mdres.pca.trb.cd4.ln$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") 

#ggsave("final_fig/pca/cd4_trb_ln.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd4_trb_ln.svg", width = 2.6, height = 1.7)

# Thymus

prop.table.trb_filter %>% colnames
##  [1] "CD4 Lymph nodes CA Exp01"   "CD4 Lymph nodes CA Exp02"  
##  [3] "CD4 Lymph nodes CA Exp03"   "CD4 Lymph nodes CAKR Exp02"
##  [5] "CD4 Lymph nodes CAKR Exp03" "CD4 Lymph nodes WT Exp02"  
##  [7] "CD4 Lymph nodes WT Exp03"   "CD4 Thymus CA Exp01"       
##  [9] "CD4 Thymus CA Exp03"        "CD4 Thymus CAKR Exp01"     
## [11] "CD4 Thymus CAKR Exp02"      "CD4 Thymus CAKR Exp03"     
## [13] "CD4 Thymus WT Exp02"        "CD4 Thymus WT Exp03"       
## [15] "CD8 Lymph nodes CA Exp02"   "CD8 Lymph nodes CA Exp03"  
## [17] "CD8 Lymph nodes CAKR Exp02" "CD8 Lymph nodes CAKR Exp03"
## [19] "CD8 Lymph nodes WT Exp01"   "CD8 Lymph nodes WT Exp02"  
## [21] "CD8 Lymph nodes WT Exp03"   "CD8 Thymus CA Exp01"       
## [23] "CD8 Thymus CA Exp03"        "CD8 Thymus CAKR Exp02"     
## [25] "CD8 Thymus CAKR Exp03"      "CD8 Thymus WT Exp01"       
## [27] "CD8 Thymus WT Exp02"        "CD8 Thymus WT Exp03"       
## [29] "aaSeqCDR3"                  "allVHitsWithScore"         
## [31] "allDHitsWithScore"          "allJHitsWithScore"
prop.table.trb2_filter.cd4.thy <- prop.table.trb_filter[,12:18]
prop.table.trb2_filter.cd4.thy$sum <- rowSums(prop.table.trb2_filter.cd4.thy)

prop.table.trb2_filter.cd4.thy <- prop.table.trb2_filter.cd4.thy %>% filter(sum >0) %>% select(-sum)

res.pca.trb.cd4.thy <- prcomp(t(prop.table.trb2_filter.cd4.thy), scale = TRUE, center = T)

mdres.pca.trb.cd4.thy  <-  colnames(prop.table.trb_filter[,12:18])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
 
fviz_pca_ind(res.pca.trb.cd4.thy,
             col.ind = as.factor(mdres.pca.trb.cd4.thy$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.trb.cd4.thy$Organ),
                  color = as.factor(mdres.pca.trb.cd4.thy$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") 

#ggsave("final_fig/pca/cd4_trb_thy.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd4_trb_thy.svg", width = 2.6, height = 1.7)

CD8

## All

prop.table.trb2_filter.cd8 <- prop.table.trb_filter[,19:32]
prop.table.trb2_filter.cd8$sum <- rowSums(prop.table.trb2_filter.cd8)
## Error in rowSums(prop.table.trb2_filter.cd8): 'x' must be numeric
prop.table.trb2_filter.cd8 <- prop.table.trb2_filter.cd8 %>% filter(sum >0) %>% select(-sum)
## Error in `filter()`:
## ! Problem while computing `..1 = sum > 0`.
## Caused by error in `sum > 0`:
## ! comparison (6) is possible only for atomic and list types
res.pca.trb.cd8 <- prcomp(t(prop.table.trb2_filter.cd8), scale = TRUE, center = T)
## Error in colMeans(x, na.rm = TRUE): 'x' must be numeric
mdres.pca.trb.cd8  <-  colnames(prop.table.trb_filter[,19:32])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
  

# LN
prop.table.trb_filter %>% colnames
##  [1] "CD4 Lymph nodes CA Exp01"   "CD4 Lymph nodes CA Exp02"  
##  [3] "CD4 Lymph nodes CA Exp03"   "CD4 Lymph nodes CAKR Exp02"
##  [5] "CD4 Lymph nodes CAKR Exp03" "CD4 Lymph nodes WT Exp02"  
##  [7] "CD4 Lymph nodes WT Exp03"   "CD4 Thymus CA Exp01"       
##  [9] "CD4 Thymus CA Exp03"        "CD4 Thymus CAKR Exp01"     
## [11] "CD4 Thymus CAKR Exp02"      "CD4 Thymus CAKR Exp03"     
## [13] "CD4 Thymus WT Exp02"        "CD4 Thymus WT Exp03"       
## [15] "CD8 Lymph nodes CA Exp02"   "CD8 Lymph nodes CA Exp03"  
## [17] "CD8 Lymph nodes CAKR Exp02" "CD8 Lymph nodes CAKR Exp03"
## [19] "CD8 Lymph nodes WT Exp01"   "CD8 Lymph nodes WT Exp02"  
## [21] "CD8 Lymph nodes WT Exp03"   "CD8 Thymus CA Exp01"       
## [23] "CD8 Thymus CA Exp03"        "CD8 Thymus CAKR Exp02"     
## [25] "CD8 Thymus CAKR Exp03"      "CD8 Thymus WT Exp01"       
## [27] "CD8 Thymus WT Exp02"        "CD8 Thymus WT Exp03"       
## [29] "aaSeqCDR3"                  "allVHitsWithScore"         
## [31] "allDHitsWithScore"          "allJHitsWithScore"
prop.table.trb2_filter.cd8.ln <- prop.table.trb_filter[,19:25]
prop.table.trb2_filter.cd8.ln$sum <- rowSums(prop.table.trb2_filter.cd8.ln)

prop.table.trb2_filter.cd8.ln <- prop.table.trb2_filter.cd8.ln %>% filter(sum >0) %>% select(-sum)

res.pca.trb.cd8.ln <- prcomp(t(prop.table.trb2_filter.cd8.ln), scale = TRUE, center = T)

mdres.pca.trb.cd8.ln  <-  colnames(prop.table.trb_filter[,19:25])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
  
fviz_pca_ind(res.pca.trb.cd8.ln,
             col.ind = as.factor(mdres.pca.trb.cd8.ln$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.trb.cd8.ln$Organ),
                  color = as.factor(mdres.pca.trb.cd8.ln$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") 

#ggsave("final_fig/pca/cd8_trb_ln.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd8_trb_ln.svg", width = 2.6, height = 1.7)


# Thymus

prop.table.trb_filter %>% colnames
##  [1] "CD4 Lymph nodes CA Exp01"   "CD4 Lymph nodes CA Exp02"  
##  [3] "CD4 Lymph nodes CA Exp03"   "CD4 Lymph nodes CAKR Exp02"
##  [5] "CD4 Lymph nodes CAKR Exp03" "CD4 Lymph nodes WT Exp02"  
##  [7] "CD4 Lymph nodes WT Exp03"   "CD4 Thymus CA Exp01"       
##  [9] "CD4 Thymus CA Exp03"        "CD4 Thymus CAKR Exp01"     
## [11] "CD4 Thymus CAKR Exp02"      "CD4 Thymus CAKR Exp03"     
## [13] "CD4 Thymus WT Exp02"        "CD4 Thymus WT Exp03"       
## [15] "CD8 Lymph nodes CA Exp02"   "CD8 Lymph nodes CA Exp03"  
## [17] "CD8 Lymph nodes CAKR Exp02" "CD8 Lymph nodes CAKR Exp03"
## [19] "CD8 Lymph nodes WT Exp01"   "CD8 Lymph nodes WT Exp02"  
## [21] "CD8 Lymph nodes WT Exp03"   "CD8 Thymus CA Exp01"       
## [23] "CD8 Thymus CA Exp03"        "CD8 Thymus CAKR Exp02"     
## [25] "CD8 Thymus CAKR Exp03"      "CD8 Thymus WT Exp01"       
## [27] "CD8 Thymus WT Exp02"        "CD8 Thymus WT Exp03"       
## [29] "aaSeqCDR3"                  "allVHitsWithScore"         
## [31] "allDHitsWithScore"          "allJHitsWithScore"
prop.table.trb2_filter.cd8.thy <- prop.table.trb_filter[,26:32]
prop.table.trb2_filter.cd8.thy$sum <- rowSums(prop.table.trb2_filter.cd8.thy)
## Error in rowSums(prop.table.trb2_filter.cd8.thy): 'x' must be numeric
prop.table.trb2_filter.cd8.thy <- prop.table.trb2_filter.cd8.thy %>% filter(sum >0) %>% select(-sum)
## Error in `filter()`:
## ! Problem while computing `..1 = sum > 0`.
## Caused by error in `sum > 0`:
## ! comparison (6) is possible only for atomic and list types
res.pca.trb.cd8.thy <- prcomp(t(prop.table.trb2_filter.cd8.thy), scale = TRUE, center = T)
## Error in colMeans(x, na.rm = TRUE): 'x' must be numeric
mdres.pca.trb.cd8.thy  <-  colnames(prop.table.trb_filter[,26:32])  %>% 
  as.data.frame() %>% 
  mutate(sample = stringr::str_replace_all(., pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp"))
 
fviz_pca_ind(res.pca.trb.cd8.thy,
             col.ind = as.factor(mdres.pca.trb.cd8.thy$Strain), # color by groups
             legend.title = "Groups",
             geom.ind = "point",
             invisible="quali", pointsize = 0) +
   scale_color_manual(values=c("dodgerblue","indianred2","gray40")) +
   geom_point(aes(shape = as.factor(mdres.pca.trb.cd8.thy$Organ),
                  color = as.factor(mdres.pca.trb.cd8.thy$Strain))) +
  geom_hline(yintercept = 0, color = "grey20") +
  geom_vline(xintercept = 0, color = "grey20") 
## Error in .get_facto_class(X): object 'res.pca.trb.cd8.thy' not found
#ggsave("final_fig/pca/cd8_trb_thy.png", width = 2.6, height = 1.7)
#ggsave("final_fig/pca/cd8_trb_thy.svg", width = 2.6, height = 1.7)

Diversity index

immdata_tra_wonkt <- repFilter(immdata_tra, "by.clonotype",
  list(V.name = exclude("TRAV11"), J.name = exclude("TRAJ18")),
  .match = "substring")

## ALL TRA
repDiversity(immdata_tra_wonkt$data) %>% vis(.by = c( "Mouse_strain"), .meta = immdata_tra$meta, 
  .signif.label.size = 0) + ggtitle("TRA without NKT") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70")) + ggtitle("all TRA", subtitle = "")

#ggsave("./final_fig/diversity/chao_tra_wonkt_sign.png", width = 8, height = 13, units = "cm")
#ggsave("./final_fig/diversity/chao_tra_wonkt_sign.svg", width = 8, height = 13, units = "cm")


## ALL TRB
repDiversity(immdata_trb$data) %>% vis(.by = c( "Mouse_strain"), .meta = immdata_trb$meta, 
  .signif.label.size = 0) + ggtitle("TRB without NKT") + scale_fill_manual(values = c("dodgerblue","indianred2","gray70")) + ggtitle("all TRB", subtitle = "")

#ggsave("./final_fig/diversity/chao_trb_sign.png", width = 8, height = 13, units = "cm")
#ggsave("./final_fig/diversity/chao_trb_sign.svg", width = 8, height = 13, units = "cm")


## Chao index
repDiversity(immdata_tra_wonkt$data) %>% vis(.by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_tra$meta, .test = F,
  .signif.label.size = 0) + ggtitle("TRA")

#ggsave("./plots/chao_tra.png", width = 15, height = 11, units = "cm")
#ggsave("./plots/chao_tra.svg", width = 15, height = 11, units = "cm")

repDiversity(immdata_trb$data) %>% vis(.by = c("Cell_type", "Organ","Mouse_strain"), .meta = immdata_trb$meta, .test = F,
  .signif.label.size = 0) + ggtitle("TRB", subtitle = "")

#ggsave("./plots/chao_trb.png", width = 15, height = 11, units = "cm")
#ggsave("./plots/chao_trb.svg", width = 15, height = 11, units = "cm")
chao_tra <- as.data.frame(repDiversity(immdata_tra_wonkt$data))
chao_trb <- as.data.frame(repDiversity(immdata_trb$data))
chao_tra2 <- chao_tra %>% 
  rownames_to_column("num_id") %>% mutate(num_id = str_replace_all(string = num_id, c("new_lib_24" = "lib24_S24_L001",  "new_lib_25" = "lib25_S25_L001"))) %>% 
  separate(num_id, into = c("num_id",NA,NA), sep = "_") %>% 
  mutate(num_id = str_replace(num_id, "lib","")) %>% 
  mutate_at(num_id, as.numeric) 
## Error in check_dot_cols(.vars, .cols): object 'num_id' not found
levels_cd4 <- c("CD4 Thymus WT", "CD4 Thymus CA", "CD4 Thymus CAKR", "CD4 Lymph nodes WT", "CD4 Lymph nodes CA", "CD4 Lymph nodes CAKR")

# Attach metadata sent for sequencing

chao_tra2 <- left_join(chao_tra2, md) %>% select(value = Estimator, Cell_type, Organ, Mouse_strain) %>%
   mutate(sample_type = paste(Cell_type, Organ, Mouse_strain)) 
## Error in left_join(chao_tra2, md): object 'chao_tra2' not found
chao_tra2 %>% filter(Cell_type == "CD4") %>% 
  ggplot(aes(y = value, x = factor(sample_type, levels = levels_cd4))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Chao index estimate") + 
  ggtitle("CD4 TRA") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
## Error in filter(., Cell_type == "CD4"): object 'chao_tra2' not found
#ggsave("./final_fig/diversity/chao_cd4_tra.png", width = 8, height = 10, units = "cm")
#ggsave("./final_fig/diversity/chao_cd4_tra.eps", width = 8, height = 10, units = "cm")

levels_cd8 <- c("CD8 Thymus WT", "CD8 Thymus CA", "CD8 Thymus CAKR", "CD8 Lymph nodes WT", "CD8 Lymph nodes CA", "CD8 Lymph nodes CAKR")

chao_tra2 %>% filter(Cell_type == "CD8") %>% 
  ggplot(aes(y = value, x = factor(sample_type, levels = levels_cd8))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Chao index estimate") + 
  ggtitle("CD8 TRA") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
## Error in filter(., Cell_type == "CD8"): object 'chao_tra2' not found
#ggsave("./final_fig/diversity/chao_cd8_tra.png", width = 8, height = 10, units = "cm")
#ggsave("./final_fig/diversity/chao_cd8_tra.eps", width = 8, height = 10, units = "cm")
chao_trb2 <- chao_trb %>% 
  rownames_to_column("num_id") %>% mutate(num_id = str_replace_all(string = num_id, c("new_lib_24" = "lib24_S24_L001",  "new_lib_25" = "lib25_S25_L001"))) %>% 
  separate(num_id, into = c("num_id",NA,NA), sep = "_") %>% 
  mutate(num_id = str_replace(num_id, "lib",""))  %>% 
  mutate_at(num_id, as.numeric) 
## Error in check_dot_cols(.vars, .cols): object 'num_id' not found
levels_cd4 <- c("CD4 Thymus WT", "CD4 Thymus CA", "CD4 Thymus CAKR", "CD4 Lymph nodes WT", "CD4 Lymph nodes CA", "CD4 Lymph nodes CAKR")

chao_trb2 <- left_join(chao_trb2, md) %>% select(value = Estimator, Cell_type, Organ, Mouse_strain) %>%
   mutate(sample_type = paste(Cell_type, Organ, Mouse_strain)) 
## Error in left_join(chao_trb2, md): object 'chao_trb2' not found
chao_trb2 %>% filter(Cell_type == "CD4") %>% 
  ggplot(aes(y = value, x = factor(sample_type, levels = levels_cd4))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Chao index estimate") + 
  ggtitle("CD4 TRB") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,125500)) + xlab("")
## Error in filter(., Cell_type == "CD4"): object 'chao_trb2' not found
#ggsave("./final_fig/diversity/chao_cd4_trb.png", width = 8, height = 10, units = "cm")
#ggsave("./final_fig/diversity/chao_cd4_trb.eps", width = 8, height = 10, units = "cm")

levels_cd8 <- c("CD8 Thymus WT", "CD8 Thymus CA", "CD8 Thymus CAKR", "CD8 Lymph nodes WT", "CD8 Lymph nodes CA", "CD8 Lymph nodes CAKR")


chao_trb2 %>% filter(Cell_type == "CD8") %>% 
  ggplot(aes(y = value, x = factor(sample_type, levels = levels_cd8))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Mouse_strain)) + 
  ylab("Chao index estimate") + 
  ggtitle("CD8 TRB") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,125500)) + xlab("")
## Error in filter(., Cell_type == "CD8"): object 'chao_trb2' not found
#ggsave("./final_fig/diversity/chao_cd8_trb.png", width = 8, height = 10, units = "cm")
#ggsave("./final_fig/diversity/chao_cd8_trb.eps", width = 8, height = 10, units = "cm")
chao_trb2 %>% 
  ggplot(aes(y = value, x = factor(Mouse_strain, levels = c("WT","CA","CAKR")))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.1), size = 2, aes(color = Mouse_strain)) +
ylab("Chao index estimate") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
## Error in ggplot(., aes(y = value, x = factor(Mouse_strain, levels = c("WT", : object 'chao_trb2' not found
#ggsave("./final_fig/diversity/chao_all_trb.png", width = 8, height = 6, units = "cm")
#ggsave("./final_fig/diversity/chao_all_trb.eps", width = 8, height = 6, units = "cm")


chao_tra2 %>% 
  ggplot(aes(y = value, x = factor(Mouse_strain, levels = c("WT","CA","CAKR")))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.1), size = 2, aes(color = Mouse_strain)) +
ylab("Chao index estimate") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")
## Error in ggplot(., aes(y = value, x = factor(Mouse_strain, levels = c("WT", : object 'chao_tra2' not found
#ggsave("./final_fig/diversity/chao_all_tra.png", width = 8, height = 6, units = "cm")
#ggsave("./final_fig/diversity/chao_all_tra.eps", width = 8, height = 6, units = "cm")

Top20 WT LN sequences

topclones <- prop.table.trb_filter %>% mutate(topclones_ln_cd4 = (`CD4 Lymph nodes WT Exp02`+
                                                                       `CD4 Lymph nodes WT Exp03`)/2,
                                               topclones_ln_cd8 = (`CD8 Lymph nodes WT Exp01`+
                                                                       `CD8 Lymph nodes WT Exp02`+
                                                                       `CD8 Lymph nodes WT Exp03`)/3)

cd4_topclones_20sequences_trb <- pull(topclones %>% slice_max(order_by = topclones_ln_cd4, n = 20), aaSeqCDR3)
cd8_topclones_20sequences_trb <- pull(topclones %>% slice_max(order_by = topclones_ln_cd8, n = 20), aaSeqCDR3)
topclones <- prop.table.tra_filter %>% mutate(topclones_ln_cd4 = (`CD4 Lymph nodes WT Exp02`+
                                                                       `CD4 Lymph nodes WT Exp03`)/2,
                                               topclones_ln_cd8 = (`CD8 Lymph nodes WT Exp01`+
                                                                       `CD8 Lymph nodes WT Exp02`+
                                                                       `CD8 Lymph nodes WT Exp03`)/3)

cd4_topclones_20sequences_tra <- pull(topclones %>% slice_max(order_by = topclones_ln_cd4, n = 20), aaSeqCDR3)
cd8_topclones_20sequences_tra <- pull(topclones %>% slice_max(order_by = topclones_ln_cd8, n = 20), aaSeqCDR3)

Summary plot

CD4 TRA

cd4_topclones_rep_pct <- prop.table.tra2 %>% filter(aaSeqCDR3 %in% cd4_topclones_20sequences_tra) %>% select(aaSeqCDR3, starts_with("CD4")) %>% select(1,14,15,9:13,7,8,2:6) 

cd4_topclones_rep_pct2 <- as.data.frame(colSums(cd4_topclones_rep_pct[,2:15]))
colnames(cd4_topclones_rep_pct2) <- "value"

cd4_topclones_rep_pct2 <- cd4_topclones_rep_pct2 %>% 
  rownames_to_column("sample") %>% 
  mutate(sample = stringr::str_replace_all(sample, pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp")) %>% 
  mutate(Cell_type_organ = factor(paste(Organ, Strain), levels = c("Thymus WT","Thymus CA", "Thymus CAKR",
                                                                   "LN WT","LN CA", "LN CAKR" )))

cd4_topclones_rep_pct2
##    Cell_type  Organ Strain   Exp      value Cell_type_organ
## 1        CD4 Thymus     WT Exp02 0.02719600       Thymus WT
## 2        CD4 Thymus     WT Exp03 0.02768635       Thymus WT
## 3        CD4 Thymus     CA Exp01 0.02961628       Thymus CA
## 4        CD4 Thymus     CA Exp03 0.02546552       Thymus CA
## 5        CD4 Thymus   CAKR Exp01 0.02006336     Thymus CAKR
## 6        CD4 Thymus   CAKR Exp02 0.02571295     Thymus CAKR
## 7        CD4 Thymus   CAKR Exp03 0.02394512     Thymus CAKR
## 8        CD4     LN     WT Exp02 0.03291915           LN WT
## 9        CD4     LN     WT Exp03 0.03092514           LN WT
## 10       CD4     LN     CA Exp01 0.03194544           LN CA
## 11       CD4     LN     CA Exp02 0.03571429           LN CA
## 12       CD4     LN     CA Exp03 0.03472770           LN CA
## 13       CD4     LN   CAKR Exp02 0.03056850         LN CAKR
## 14       CD4     LN   CAKR Exp03 0.02854483         LN CAKR
cd4_topclones_rep_pct2 %>% 
  filter(Organ == "LN") %>% 
  ggplot(aes(y = value*100, x = factor(Cell_type_organ))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Strain)) + 
  ylab("% of repertoire") + 
  ggtitle("CD4 TRA") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")

#ggsave("./final_fig/20topseqs/20topseqs_pct_tra_cd4.png", width = 5, height = 5.7, units = "cm")
#ggsave("./final_fig/20topseqs/20topseqs_pct_tra_cd4.eps", width = 5, height = 5.7, units = "cm")

CD4 TRB

cd4_topclones_rep_pct <- prop.table.trb2 %>% filter(aaSeqCDR3 %in% cd4_topclones_20sequences_trb) %>% select(aaSeqCDR3, starts_with("CD4")) %>% select(1,14,15,9:13,7,8,2:6) 

cd4_topclones_rep_pct2 <- as.data.frame(colSums(cd4_topclones_rep_pct[,2:15]))
colnames(cd4_topclones_rep_pct2) <- "value"

cd4_topclones_rep_pct2 <- cd4_topclones_rep_pct2 %>% 
  rownames_to_column("sample") %>% 
  mutate(sample = stringr::str_replace_all(sample, pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp")) %>% 
  mutate(Cell_type_organ = factor(paste(Organ, Strain), levels = c("Thymus WT","Thymus CA", "Thymus CAKR",
                                                                   "LN WT","LN CA", "LN CAKR" )))

cd4_topclones_rep_pct2
##    Cell_type  Organ Strain   Exp       value Cell_type_organ
## 1        CD4 Thymus     WT Exp02 0.002983516       Thymus WT
## 2        CD4 Thymus     WT Exp03 0.003173193       Thymus WT
## 3        CD4 Thymus     CA Exp01 0.002918556       Thymus CA
## 4        CD4 Thymus     CA Exp03 0.002984742       Thymus CA
## 5        CD4 Thymus   CAKR Exp01 0.002182929     Thymus CAKR
## 6        CD4 Thymus   CAKR Exp02 0.001994960     Thymus CAKR
## 7        CD4 Thymus   CAKR Exp03 0.002340585     Thymus CAKR
## 8        CD4     LN     WT Exp02 0.007174320           LN WT
## 9        CD4     LN     WT Exp03 0.004953674           LN WT
## 10       CD4     LN     CA Exp01 0.004225770           LN CA
## 11       CD4     LN     CA Exp02 0.004535615           LN CA
## 12       CD4     LN     CA Exp03 0.005438813           LN CA
## 13       CD4     LN   CAKR Exp02 0.003940359         LN CAKR
## 14       CD4     LN   CAKR Exp03 0.003868959         LN CAKR
cd4_topclones_rep_pct2 %>% 
  filter(Organ == "LN") %>% 
  ggplot(aes(y = value*100, x = factor(Cell_type_organ))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Strain)) + 
  ylab("% of repertoire") + 
  ggtitle("CD4 TRB") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")

#ggsave("./final_fig/20topseqs/20topseqs_pct_trb_cd4.png", width = 5.4, height = 5.7, units = "cm")
#ggsave("./final_fig/20topseqs/20topseqs_pct_trb_cd4.eps", width = 5.4, height = 5.7, units = "cm")

CD8 TRA

cd8_topclones_rep_pct <- prop.table.tra2 %>% filter(aaSeqCDR3 %in% cd8_topclones_20sequences_tra) %>% select(aaSeqCDR3, starts_with("CD8")) %>% select(1,13:15,9:12,6:8,2:5) 

cd8_topclones_rep_pct2 <- as.data.frame(colSums(cd8_topclones_rep_pct[,2:15]))
colnames(cd8_topclones_rep_pct2) <- "value"

cd8_topclones_rep_pct2 <- cd8_topclones_rep_pct2 %>% 
  rownames_to_column("sample") %>% 
  mutate(sample = stringr::str_replace_all(sample, pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp")) %>% 
  mutate(Cell_type_organ = factor(paste(Organ, Strain), levels = c("Thymus WT","Thymus CA", "Thymus CAKR",
                                                                   "LN WT","LN CA", "LN CAKR" )))

cd8_topclones_rep_pct2
##    Cell_type  Organ Strain   Exp      value Cell_type_organ
## 1        CD8 Thymus     WT Exp01 0.03400861       Thymus WT
## 2        CD8 Thymus     WT Exp02 0.02653846       Thymus WT
## 3        CD8 Thymus     WT Exp03 0.03137384       Thymus WT
## 4        CD8 Thymus     CA Exp01 0.02447936       Thymus CA
## 5        CD8 Thymus     CA Exp03 0.02870174       Thymus CA
## 6        CD8 Thymus   CAKR Exp02 0.03063281     Thymus CAKR
## 7        CD8 Thymus   CAKR Exp03 0.02280437     Thymus CAKR
## 8        CD8     LN     WT Exp01 0.03753115           LN WT
## 9        CD8     LN     WT Exp02 0.03764212           LN WT
## 10       CD8     LN     WT Exp03 0.03642344           LN WT
## 11       CD8     LN     CA Exp02 0.03592586           LN CA
## 12       CD8     LN     CA Exp03 0.03213014           LN CA
## 13       CD8     LN   CAKR Exp02 0.02946860         LN CAKR
## 14       CD8     LN   CAKR Exp03 0.03059997         LN CAKR
cd8_topclones_rep_pct2 %>% 
  filter(Organ == "LN") %>% 
  ggplot(aes(y = value*100, x = factor(Cell_type_organ))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Strain)) + 
  ylab("% of repertoire") + 
  ggtitle("CD8 TRA") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")

#ggsave("./final_fig/20topseqs/20topseqs_pct_tra_cd8.png", width = 5, height = 5.7, units = "cm")
#ggsave("./final_fig/20topseqs/20topseqs_pct_tra_cd8.eps", width = 5, height = 5.7, units = "cm")

CD8 TRB

cd8_topclones_rep_pct <- prop.table.trb2 %>% filter(aaSeqCDR3 %in% cd8_topclones_20sequences_trb) %>% select(aaSeqCDR3, starts_with("CD8")) %>% select(1,13:15,9:12,6:8,2:5) 

cd8_topclones_rep_pct2 <- as.data.frame(colSums(cd8_topclones_rep_pct[,2:15]))
colnames(cd8_topclones_rep_pct2) <- "value"

cd8_topclones_rep_pct2 <- cd8_topclones_rep_pct2 %>% 
  rownames_to_column("sample") %>% 
  mutate(sample = stringr::str_replace_all(sample, pattern = "Lymph nodes", replacement = "LN")) %>% 
  separate(sample, into = c("Cell_type","Organ","Strain","Exp")) %>% 
  mutate(Cell_type_organ = factor(paste(Organ, Strain), levels = c("Thymus WT","Thymus CA", "Thymus CAKR",
                                                                   "LN WT","LN CA", "LN CAKR" )))

cd8_topclones_rep_pct2
##    Cell_type  Organ Strain   Exp       value Cell_type_organ
## 1        CD8 Thymus     WT Exp01 0.004631440       Thymus WT
## 2        CD8 Thymus     WT Exp02 0.004479484       Thymus WT
## 3        CD8 Thymus     WT Exp03 0.004929262       Thymus WT
## 4        CD8 Thymus     CA Exp01 0.005472183       Thymus CA
## 5        CD8 Thymus     CA Exp03 0.004517467       Thymus CA
## 6        CD8 Thymus   CAKR Exp02 0.003983029     Thymus CAKR
## 7        CD8 Thymus   CAKR Exp03 0.005094131     Thymus CAKR
## 8        CD8     LN     WT Exp01 0.007470700           LN WT
## 9        CD8     LN     WT Exp02 0.006188663           LN WT
## 10       CD8     LN     WT Exp03 0.007226656           LN WT
## 11       CD8     LN     CA Exp02 0.006775749           LN CA
## 12       CD8     LN     CA Exp03 0.007135684           LN CA
## 13       CD8     LN   CAKR Exp02 0.007553967         LN CAKR
## 14       CD8     LN   CAKR Exp03 0.006905450         LN CAKR
cd8_topclones_rep_pct2 %>% 
  filter(Organ == "LN") %>% 
  ggplot(aes(y = value*100, x = factor(Cell_type_organ))) +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  geom_point(aes(color = Strain)) + 
  ylab("% of repertoire") + 
  ggtitle("CD8 TRB") + 
  theme(axis.text.x = element_text(angle = 90)) + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) +
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,NA)) + xlab("")

#ggsave("./final_fig/20topseqs/20topseqs_pct_trb_cd8.png", width = 5.4, height = 5.7, units = "cm")
#ggsave("./final_fig/20topseqs/20topseqs_pct_trb_cd8.eps", width = 5.4, height = 5.7, units = "cm")

Heatmaps

TRA

CD4

# TRA
cd4_topclones_heatmap_matrix <- prop.table.tra2 %>% filter(aaSeqCDR3 %in% cd4_topclones_20sequences_tra) %>% select(aaSeqCDR3, starts_with("CD4")) %>% select(1,14,15,9:13,7,8,2:6)

cd4_topclones_heatmap_matrix2 <- as.matrix(cd4_topclones_heatmap_matrix[,2:15])
rownames(cd4_topclones_heatmap_matrix2) <- cd4_topclones_heatmap_matrix$aaSeqCDR3

cd4_topclones_heatmap_matrix2 <- cd4_topclones_heatmap_matrix2[match( cd4_topclones_20sequences_tra, rownames(cd4_topclones_heatmap_matrix2)),]

pheatmap(cd4_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F)

#ggsave(plot = pheatmap(cd4_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F), "final_fig/heatmap_top20_ordered/cd4_tra_noscale.svg")

CD8

# TRA

cd8_topclones_heatmap_matrix <- prop.table.tra2 %>% filter(aaSeqCDR3 %in% cd8_topclones_20sequences_tra) %>% select(aaSeqCDR3, starts_with("CD8")) %>% select(1,13:15,9:12,6:8,2:5)
  

cd8_topclones_heatmap_matrix2 <- as.matrix(cd8_topclones_heatmap_matrix[,2:15])
rownames(cd8_topclones_heatmap_matrix2) <- cd8_topclones_heatmap_matrix$aaSeqCDR3

cd8_topclones_heatmap_matrix2 <- cd8_topclones_heatmap_matrix2[match( cd8_topclones_20sequences_tra, rownames(cd8_topclones_heatmap_matrix2)),]

pheatmap(cd8_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F)

#ggsave(plot = pheatmap(cd8_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F), "final_fig/heatmap_top20_ordered/cd8_tra_noscale.svg")

without 1st sequence

### without 1st sequence

cd8_topclones_heatmap_matrix2 <- as.matrix(cd8_topclones_heatmap_matrix[2:20,2:15])
rownames(cd8_topclones_heatmap_matrix2) <- cd8_topclones_heatmap_matrix$aaSeqCDR3[2:20]

cd8_topclones_heatmap_matrix2 <- cd8_topclones_heatmap_matrix2[match( cd8_topclones_20sequences_tra[2:20], rownames(cd8_topclones_heatmap_matrix2)),]

pheatmap(cd8_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F)

#ggsave(plot = pheatmap(cd8_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F), "final_fig/heatmap_top20_ordered/cd8_tra_noscale_without.svg")

TRB

CD4

# trb

cd4_topclones_heatmap_matrix <- prop.table.trb2 %>% filter(aaSeqCDR3 %in% cd4_topclones_20sequences_trb) %>% select(aaSeqCDR3, starts_with("CD4")) %>% select(1,14,15,9:13,7,8,2:6)
  
cd4_topclones_heatmap_matrix2 <- as.matrix(cd4_topclones_heatmap_matrix[,2:15])
rownames(cd4_topclones_heatmap_matrix2) <- cd4_topclones_heatmap_matrix$aaSeqCDR3

cd4_topclones_heatmap_matrix2 <- cd4_topclones_heatmap_matrix2[match( cd4_topclones_20sequences_trb, rownames(cd4_topclones_heatmap_matrix2)),]

pheatmap(cd4_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F)

#ggsave(plot = pheatmap(cd4_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F), "final_fig/heatmap_top20_ordered/cd4_trb_noscale.svg")

CD8

# trb

cd8_topclones_heatmap_matrix <- prop.table.trb2 %>% filter(aaSeqCDR3 %in% cd8_topclones_20sequences_trb) %>% select(aaSeqCDR3, starts_with("CD8")) %>% select(1,13:15,9:12,6:8,2:5)
  
cd8_topclones_heatmap_matrix2 <- as.matrix(cd8_topclones_heatmap_matrix[,2:15])
rownames(cd8_topclones_heatmap_matrix2) <- cd8_topclones_heatmap_matrix$aaSeqCDR3

cd8_topclones_heatmap_matrix2 <- cd8_topclones_heatmap_matrix2[match( cd8_topclones_20sequences_trb, rownames(cd8_topclones_heatmap_matrix2)),]

pheatmap(cd8_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F)

# png("final_fig/heatmap_top20_ordered/cd8_trb_noscale.png", width = 400, height = 500)
# pheatmap(cd8_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F)
# dev.off()

#ggsave(plot = pheatmap(cd8_topclones_heatmap_matrix2, scale = "none", cluster_rows = F, cluster_cols = F), "final_fig/heatmap_top20_ordered/cd8_trb_noscale.svg")

Repertoire overlap

binary_tra <- excel_count_table_tra %>% 
  mutate(nkt_trav11_traj18 = if_else(
    (grepl(allVHitsWithScore, pattern = "TRAV11") | 
    (grepl(allJHitsWithScore, pattern = "TRAJ18"))),"yes","no")) %>% 
  filter(nkt_trav11_traj18 == "no") %>% 
  mutate_at(vars(starts_with("CD")), .funs = binary) 

# count_overlap <- function(df, column_number){
#   repertoire <- df %>% select(vars(column_number), aaSeqCDR3) %>% filter(vars(column_number)>0) %>% pull("aaSeqCDR3")
#   nrow_column <- length(repertoire)
#   intersect_rept <- length(intersect(repertoire, most_diverse_repertoire))
#   pct <- intersect_rept/length()
#   return(x)
# }

binary_tra_longer <- binary_tra %>% select(starts_with("CD"), aaSeqCDR3) %>% pivot_longer(!aaSeqCDR3, names_to = "num_id")

df_all4 <- data.frame("")

for(j in levels(factor(binary_tra_longer$num_id))){
  subset1 <- binary_tra_longer %>% filter(num_id == j & value>0) %>% pull("aaSeqCDR3")
  vector_overlap <- c()
  for(i in levels(factor(binary_tra_longer$num_id))){
    subset2 <- binary_tra_longer %>% filter(num_id == i & value>0) %>% pull("aaSeqCDR3")
    intersect_rep <- length(intersect(subset1, subset2))
    total <- length(subset2)
    vector_overlap <- c(vector_overlap,intersect_rep/total)
  }
  df <- as.data.frame(x = vector_overlap)
  colnames(df) <- j
  df
  df_all4 <- cbind(df_all4, df)
}

df_all4 <- df_all4[,2:29]
rownames(df_all4) <- colnames(df_all4)

#write.csv(df_all4, "rep_overlap_tra.csv")
df24 <- df_all4
df24[df24 == 1] <- 0

### plot dotplot of overlaps

df25 <- df24 %>% rownames_to_column("id") %>%  pivot_longer(-id)

g2 <- ggplot(df25, aes(id, factor(name, levels = rev(levels(factor(name)))))) + 
  geom_point(aes(size = value*100, colour = value*100)) + 
  theme_bw() 

g2 + scale_size_continuous(range=c(7,12)) +
  geom_text(aes(label = round(value*100, digits = 1))) + 
  scale_colour_gradient2(low = "lightskyblue", mid = "lightsteelblue2", high = "salmon") +
  theme(axis.text.x = element_text(angle = 90)) 

Rozdelene na CD4 a CD8

rep_overlap_tra <- read_csv("rep_overlap_tra.csv") %>% column_to_rownames("...1")

rep_overlap_tra_cd4 <- rep_overlap_tra[1:14,1:14]
rep_overlap_tra_cd8 <- rep_overlap_tra[15:28,15:28]

rep_overlap_tra_cd4 <- rep_overlap_tra_cd4[ rev(c(5, 4, 3, 2, 1, 7, 6, 12, 11, 10, 9, 8, 14, 13)),
                                            rev(c(5, 4, 3, 2, 1, 7, 6, 12, 11, 10, 9, 8, 14, 13))]

levels_names <- colnames(rep_overlap_tra_cd4)

df_overlap_tra_cd4 <- rep_overlap_tra_cd4
df_overlap_tra_cd4[df_overlap_tra_cd4 == 1] <- 0

### plot dotplot of overlaps

df_overlap_tra_cd4 <- df_overlap_tra_cd4 %>% rownames_to_column("id") %>%  pivot_longer(-id)

g2 <- ggplot(df_overlap_tra_cd4, aes(x = factor(name, levels = levels_names), y = factor(id, levels = rev(levels_names)))) + 
  geom_point(aes(size = value*100, colour = value*100)) + 
  theme_bw() 

g2 + scale_size_continuous(range=c(5,9)) +
  geom_text(aes(label = round(value*100, digits = 1)), size = 3) + 
  scale_colour_gradient2(low = "white", mid = "lightsteelblue", high = "salmon") +
  theme(axis.text.x = element_text(angle = 90)) + ggtheme()

#ggsave(file = "./final_fig/overlap_cd4_tra.png", width = 20, height = 16, units = "cm")
#ggsave(file = "./final_fig/overlap_cd4_tra.svg", width = 20, height = 16, units = "cm")
rep_overlap_tra_cd8 <- rep_overlap_tra_cd8[rev(c(12:14,8:11,5:7,1:4)),rev(c(12:14,8:11,5:7,1:4))]

levels_names <- rev(colnames(rep_overlap_tra_cd8))

df_overlap_tra_cd8 <- rep_overlap_tra_cd8
df_overlap_tra_cd8[df_overlap_tra_cd8 == 1] <- 0

### plot dotplot of overlaps

df_overlap_tra_cd8 <- df_overlap_tra_cd8 %>% rownames_to_column("id") %>%  pivot_longer(-id)

g2 <- ggplot(df_overlap_tra_cd8, aes(factor(name, levels = levels_names), factor(id, levels = rev(levels_names)))) + 
  geom_point(aes(size = value*100, colour = value*100)) + 
  theme_bw() 

g2 + scale_size_continuous(range=c(5,9)) +
  geom_text(aes(label = round(value*100, digits = 1)), size = 3) + 
  scale_colour_gradient2(low = "white", mid = "lightsteelblue", high = "salmon") +
  theme(axis.text.x = element_text(angle = 90)) + ggtheme()

#ggsave(file = "./final_fig/overlap_cd8_tra.png", width = 20, height = 16, units = "cm")
#ggsave(file = "./final_fig/overlap_cd8_tra.svg", width = 20, height = 16, units = "cm")

A la violin

violin_tra_cd4 <- rep_overlap_tra_cd4 %>% 
  rownames_to_column("sample1") %>% 
  pivot_longer(!sample1, names_to = "sample2") %>% 
  mutate(sample2 = stringr::str_replace_all(sample2, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  mutate(sample1 = stringr::str_replace_all(sample1, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  separate(sample1, into = c("Cell_type1","Organ1","Strain1","Exp1"), remove = F)  %>% 
  separate(sample2, into = c("Cell_type2","Organ2","Strain2","Exp2"), remove = F) %>% 
  filter(value<1)


violin_tra_cd8 <- rep_overlap_tra_cd8 %>% 
  rownames_to_column("sample1") %>% 
  pivot_longer(!sample1, names_to = "sample2") %>% 
  mutate(sample2 = stringr::str_replace_all(sample2, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  mutate(sample1 = stringr::str_replace_all(sample1, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  separate(sample1, into = c("Cell_type1","Organ1","Strain1","Exp1"), remove = F)  %>% 
  separate(sample2, into = c("Cell_type2","Organ2","Strain2","Exp2"), remove = F) %>% 
  filter(value<1)
violin_tra_cd4 %>% filter(Organ1 == "Thymus" & Organ2 == "Thymus") %>% 
  mutate(organ_strain1 = paste(Organ1, Strain1), 
                          organ_strain2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strain1, organ_strain2, sep = " - ")) %>% 
  filter(comparison %in% c("Thymus WT - Thymus WT", "Thymus CA - Thymus WT", "Thymus CAKR - Thymus WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD4 TRA Thymus") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.35)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd4_tra_thy.png", width = 9.5, height = 9.5,  units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd4_tra_thy.eps", width = 9.5, height = 9.5,  units = "cm")
violin_tra_cd4 %>% filter(Organ1 == "LN" & Organ2 == "LN") %>% 
  mutate(organ_strain1 = paste(Organ1, Strain1), 
                          organ_strain2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strain1, organ_strain2, sep = " - ")) %>% 
  filter(comparison %in% c("LN WT - LN WT", "LN CA - LN WT", "LN CAKR - LN WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD4 TRA LN") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.35)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd4_tra_LN.png", width = 8, height = 8,  units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd4_tra_LN.eps", width = 8, height = 8,  units = "cm")
violin_tra_cd8 %>% filter(Organ1 == "Thymus" & Organ2 == "Thymus") %>% 
  mutate(organ_strain1 = paste(Organ1, Strain1), 
                          organ_strain2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strain1, organ_strain2, sep = " - ")) %>% 
  filter(comparison %in% c("Thymus WT - Thymus WT", "Thymus CA - Thymus WT", "Thymus CAKR - Thymus WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD8 TRA Thymus") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.35)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd8_tra_thy.png", width = 9.5, height = 9.5,  units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd8_tra_thy.eps", width = 9.5, height = 9.5,  units = "cm")
violin_tra_cd8 %>% filter(Organ1 == "LN" & Organ2 == "LN") %>% 
  mutate(organ_strain1 = paste(Organ1, Strain1), 
                          organ_strain2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strain1, organ_strain2, sep = " - ")) %>% 
  filter(comparison %in% c("LN WT - LN WT", "LN CA - LN WT", "LN CAKR - LN WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD8 TRA LN") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.35)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd8_tra_LN.png", width = 8, height = 8, units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd8_tra_LN.eps", width = 8, height = 8, units = "cm")

TRB

Vsechny vzorky se vsema

binary_trb <- excel_count_table_trb %>% 
  mutate_at(vars(starts_with("CD")), .funs = binary) 

binary_trb_longer <- binary_trb %>% select(starts_with("CD"), aaSeqCDR3) %>% pivot_longer(!aaSeqCDR3, names_to = "num_id")

df_all4 <- data.frame("")

for(j in levels(factor(binary_trb_longer$num_id))){
  subset1 <- binary_trb_longer %>% filter(num_id == j & value>0) %>% pull("aaSeqCDR3")
  vector_overlap <- c()
  for(i in levels(factor(binary_trb_longer$num_id))){
    subset2 <- binary_trb_longer %>% filter(num_id == i & value>0) %>% pull("aaSeqCDR3")
    intersect_rep <- length(intersect(subset1, subset2))
    total <- length(subset2)
    vector_overlap <- c(vector_overlap,intersect_rep/total)
  }
  df <- as.data.frame(x = vector_overlap)
  colnames(df) <- j
  df
  df_all4 <- cbind(df_all4, df)
}

df_all4 <- df_all4[,2:29]
rownames(df_all4) <- colnames(df_all4)

#write.csv(df_all4, "rep_overlap_trb.csv")
df24 <- df_all4
df24[df24 == 1] <- 0

### plot dotplot of overlaps

df25 <- df24 %>% rownames_to_column("id") %>%  pivot_longer(-id)

g2 <- ggplot(df25, aes(id, factor(name, levels = rev(levels(factor(name)))))) + 
  geom_point(aes(size = value*100, colour = value*100)) + 
  theme_bw() 

g2 + scale_size_continuous(range=c(7,12)) +
  geom_text(aes(label = round(value*100, digits = 1))) + 
  scale_colour_gradient2(low = "lightskyblue", mid = "lightsteelblue2", high = "salmon") +
  theme(axis.text.x = element_text(angle = 90)) 

Rozdelene na CD4 a CD8

rep_overlap_trb <- read_csv("rep_overlap_trb.csv") %>% column_to_rownames("...1")

rep_overlap_trb_cd4 <- rep_overlap_trb[1:14,1:14]
rep_overlap_trb_cd8 <- rep_overlap_trb[15:28,15:28]

rep_overlap_trb_cd4 <- rep_overlap_trb_cd4[rev(c(13,14,8:12,6,7,1:5)),rev(c(13,14,8:12,6,7,1:5))]

levels_names <- rev(colnames(rep_overlap_trb_cd4))


df_overlap_trb_cd4 <- rep_overlap_trb_cd4
df_overlap_trb_cd4[df_overlap_trb_cd4 == 1] <- 0

### plot dotplot of overlaps

df_overlap_trb_cd4 <- df_overlap_trb_cd4 %>% rownames_to_column("id") %>%  pivot_longer(-id)

g2 <- ggplot(df_overlap_trb_cd4, aes(factor(name, levels = levels_names), factor(id, levels = rev(levels_names)))) + 
  geom_point(aes(size = value*100, colour = value*100)) + 
  theme_bw() 

g2 + scale_size_continuous(range=c(5,9)) +
  geom_text(aes(label = round(value*100, digits = 1)), size = 3) + 
  scale_colour_gradient2(low = "white", mid = "lightsteelblue", high = "salmon") +
  theme(axis.text.x = element_text(angle = 90)) + ggtheme()

#ggsave(file = "./final_fig/overlap_cd4_trb.png", width = 20, height = 16, units = "cm")
#ggsave(file = "./final_fig/overlap_cd4_trb.svg", width = 20, height = 16, units = "cm")
rep_overlap_trb_cd8 <- rep_overlap_trb_cd8[rev(c(12:14,8:11,5:7,1:4)),rev(c(12:14,8:11,5:7,1:4))]

levels_names <- rev(colnames(rep_overlap_trb_cd8))

df_overlap_trb_cd8 <- rep_overlap_trb_cd8
df_overlap_trb_cd8[df_overlap_trb_cd8 == 1] <- 0

### plot dotplot of overlaps

df_overlap_trb_cd8 <- df_overlap_trb_cd8 %>% rownames_to_column("id") %>%  pivot_longer(-id)

g2 <- ggplot(df_overlap_trb_cd8, aes(factor(name, levels = levels_names), factor(id, levels = rev(levels_names)))) + 
  geom_point(aes(size = value*100, colour = value*100)) + 
  theme_bw() 

g2 + scale_size_continuous(range=c(5,9)) +
  geom_text(aes(label = round(value*100, digits = 1)), size = 3) + 
  scale_colour_gradient2(low = "white", mid = "lightsteelblue", high = "salmon") +
  theme(axis.text.x = element_text(angle = 90)) + ggtheme()

#ggsave(file = "./final_fig/overlap_cd8_trb.png", width = 20, height = 16, units = "cm")
#ggsave(file = "./final_fig/overlap_cd8_trb.svg", width = 20, height = 16, units = "cm")

A la violin

violin_trb_cd4 <- rep_overlap_trb_cd4 %>% 
  rownames_to_column("sample1") %>% 
  pivot_longer(!sample1, names_to = "sample2") %>% 
  mutate(sample2 = stringr::str_replace_all(sample2, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  mutate(sample1 = stringr::str_replace_all(sample1, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  separate(sample1, into = c("Cell_type1","Organ1","Strain1","Exp1"), remove = F)  %>% 
  separate(sample2, into = c("Cell_type2","Organ2","Strain2","Exp2"), remove = F) %>% 
  filter(value<1)

violin_trb_cd8 <- rep_overlap_trb_cd8 %>% 
  rownames_to_column("sample1") %>% 
  pivot_longer(!sample1, names_to = "sample2") %>% 
  mutate(sample2 = stringr::str_replace_all(sample2, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  mutate(sample1 = stringr::str_replace_all(sample1, pattern = "Lymph nodes", replacement = "LN"))  %>% 
  separate(sample1, into = c("Cell_type1","Organ1","Strain1","Exp1"), remove = F)  %>% 
  separate(sample2, into = c("Cell_type2","Organ2","Strain2","Exp2"), remove = F) %>% 
  filter(value<1)
violin_trb_cd8 %>% filter(Organ1 == "Thymus" & Organ2 == "Thymus") %>% 
  mutate(organ_strbin1 = paste(Organ1, Strain1), 
                          organ_strbin2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strbin1, organ_strbin2, sep = " - ")) %>% 
  filter(comparison %in% c("Thymus WT - Thymus WT", "Thymus CA - Thymus WT", "Thymus CAKR - Thymus WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD8 TRB Thymus") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.25)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd8_trb_thy.png", width = 9.5, height = 9.5,  units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd8_trb_thy.eps", width = 9.5, height = 9.5,  units = "cm")
violin_trb_cd8 %>% filter(Organ1 == "LN" & Organ2 == "LN") %>% 
  mutate(organ_strbin1 = paste(Organ1, Strain1), 
                          organ_strbin2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strbin1, organ_strbin2, sep = " - ")) %>% 
  filter(comparison %in% c("LN WT - LN WT", "LN CA - LN WT", "LN CAKR - LN WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD8 TRB LN") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.25)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd8_trb_LN.png", width = 8, height = 8,  units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd8_trb_LN.eps", width = 8, height = 8,  units = "cm")
violin_trb_cd4 %>% filter(Organ1 == "Thymus" & Organ2 == "Thymus") %>% 
  mutate(organ_strbin1 = paste(Organ1, Strain1), 
                          organ_strbin2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strbin1, organ_strbin2, sep = " - ")) %>% 
  filter(comparison %in% c("Thymus WT - Thymus WT", "Thymus CA - Thymus WT", "Thymus CAKR - Thymus WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD4 TRB Thymus") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.20)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd4_trb_thy.png", width = 9.5, height = 9.5,  units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd4_trb_thy.eps", width = 9.5, height = 9.5,  units = "cm")
violin_trb_cd4 %>% filter(Organ1 == "LN" & Organ2 == "LN") %>% 
  mutate(organ_strbin1 = paste(Organ1, Strain1), 
                          organ_strbin2 = paste(Organ2, Strain2)) %>% 
  mutate(comparison = paste(organ_strbin1, organ_strbin2, sep = " - ")) %>% 
  filter(comparison %in% c("LN WT - LN WT", "LN CA - LN WT", "LN CAKR - LN WT")) %>% 
  ggplot(aes(x = comparison, y = value)) +
  geom_dotplot(binaxis='y', stackdir='center', dotsize=0) + 
  geom_jitter(position=position_jitter(0.2), size = 2, aes(color = comparison)) +
  ggtitle("CD4 TRB LN") +
  stat_summary(fun = "mean",
               geom = "crossbar", 
               width = 0.5,
               colour = "black") +
  ylab("% repertoire overlap") + 
  theme_classic() + 
  ggtheme() + 
  theme(axis.text.x = element_text(angle = 90)) + 
  scale_color_manual(values = c("dodgerblue","indianred2","gray40")) + 
  ylim(c(0,0.20)) + xlab("")

#ggsave("./final_fig/overlaps/rep_overlap_cd4_trb_LN.png", width = 8, height = 8,  units = "cm")
#ggsave("./final_fig/overlaps/rep_overlap_cd4_trb_LN.eps", width = 8, height = 8,  units = "cm")

SessionInfo

sessionInfo()
## R version 4.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] kableExtra_1.3.4  factoextra_1.0.7  pheatmap_1.0.12   immunarch_0.6.9  
##  [5] data.table_1.14.2 dtplyr_1.2.1      patchwork_1.1.1   cowplot_1.1.1    
##  [9] readxl_1.4.0      forcats_0.5.1     stringr_1.4.1     dplyr_1.0.9      
## [13] purrr_0.3.4       readr_2.1.2       tidyr_1.2.0       tibble_3.1.7     
## [17] ggplot2_3.3.5     tidyverse_1.3.1  
## 
## loaded via a namespace (and not attached):
##   [1] uuid_1.1-0          backports_1.4.1     circlize_0.4.15    
##   [4] fastmatch_1.1-3     systemfonts_1.0.4   plyr_1.8.7         
##   [7] igraph_1.3.4        digest_0.6.29       foreach_1.5.2      
##  [10] htmltools_0.5.3     ggalluvial_0.12.3   fansi_1.0.3        
##  [13] magrittr_2.0.3      cluster_2.1.4       doParallel_1.0.17  
##  [16] tzdb_0.3.0          modelr_0.1.8        vroom_1.5.7        
##  [19] svglite_2.1.0       rmdformats_1.0.3    colorspace_2.0-3   
##  [22] rvest_1.0.2         ggrepel_0.9.1       haven_2.5.0        
##  [25] xfun_0.31           crayon_1.5.1        jsonlite_1.8.0     
##  [28] phangorn_2.9.0      iterators_1.0.14    ape_5.6-2          
##  [31] glue_1.6.2          gtable_0.3.0        webshot_0.5.3      
##  [34] UpSetR_1.4.0        car_3.1-0           kernlab_0.9-31     
##  [37] shape_1.4.6         prabclus_2.3-2      DEoptimR_1.0-11    
##  [40] abind_1.4-5         scales_1.2.1        DBI_1.1.3          
##  [43] rstatix_0.7.0       Rcpp_1.0.9          viridisLite_0.4.1  
##  [46] xtable_1.8-4        bit_4.0.4           mclust_5.4.10      
##  [49] stats4_4.2.1        httr_1.4.4          RColorBrewer_1.1-3 
##  [52] fpc_2.2-9           modeltools_0.2-23   ellipsis_0.3.2     
##  [55] pkgconfig_2.0.3     flexmix_2.3-18      farver_2.1.1       
##  [58] nnet_7.3-17         sass_0.4.2          ggseqlogo_0.1      
##  [61] dbplyr_2.2.1        utf8_1.2.2          tidyselect_1.1.2   
##  [64] labeling_0.4.2      rlang_1.0.4         reshape2_1.4.4     
##  [67] later_1.3.0         munsell_0.5.0       cellranger_1.1.0   
##  [70] tools_4.2.1         cachem_1.0.6        cli_3.3.0          
##  [73] generics_0.1.3      broom_1.0.0         evaluate_0.16      
##  [76] fastmap_1.1.0       yaml_2.3.5          knitr_1.39         
##  [79] bit64_4.0.5         fs_1.5.2            robustbase_0.95-0  
##  [82] nlme_3.1-159        mime_0.12           xml2_1.3.3         
##  [85] compiler_4.2.1      shinythemes_1.2.0   rstudioapi_0.14    
##  [88] ggsignif_0.6.3      reprex_2.0.1        bslib_0.4.0        
##  [91] stringi_1.7.8       highr_0.9           lattice_0.20-45    
##  [94] Matrix_1.4-1        vctrs_0.4.1         stringdist_0.9.8   
##  [97] pillar_1.8.1        lifecycle_1.0.1     jquerylib_0.1.4    
## [100] GlobalOptions_0.1.2 httpuv_1.6.5        R6_2.5.1           
## [103] bookdown_0.27       promises_1.2.0.1    gridExtra_2.3      
## [106] codetools_0.2-18    MASS_7.3-58.1       assertthat_0.2.1   
## [109] withr_2.5.0         rlist_0.4.6.2       diptest_0.76-0     
## [112] parallel_4.2.1      hms_1.1.2           quadprog_1.5-8     
## [115] grid_4.2.1          class_7.3-20        rmarkdown_2.14     
## [118] carData_3.0-5       ggpubr_0.4.0        shiny_1.7.1        
## [121] lubridate_1.8.0